var express = require('express');
var router = express.Router();
var qiniu = require('qiniu');
var superagent = require('superagent');
var async = require( 'async'); // 同步控制
var moment = require('moment');  // 日期格式化
var SJGReplaceItemText = require('./../../sjg/kernel/replace/SJGReplaceItemText');
var SJGReplaceItemTableRow = require('./../../sjg/kernel/replace/SJGReplaceItemTableRow');
var SJGReplaceItemTableCell = require('./../../sjg/kernel/replace/SJGReplaceItemTableCell');

// 分词模块
var Segment = require('node-segment').Segment;// 载入分词模块
var POSTAG = require('node-segment').POSTAG;// 载入词性接口
var segment = new Segment();// 创建实例
segment.useDefault();// 使用默认的识别模块及字典，载入字典文件需要1秒，仅初始化时执行一次即可



var SJGNode = require('./../../sjg/common/tg/SJGNode');
var SJGTG = require('./../../sjg/common/tg/SJGTG');
var SJGTGV3 = require('./../../sjg/common/tg3/SJGTGV3');
var SJGNodeV3 = require('./../../sjg/common/tg3/SJGNodeV3');
var GlobalData = require('./../../sjg/z/GlobalData');
var SJGPage = require('./../../sjg/kernel/cont/SJGPage');
var GCJ_CLASS_TYPE = require('./../../sjg/common/GCJ_CLASS_TYPE');
//var GroupObj = require('./../../sjg/kernel/obj/GroupObj');
//var TextExtObj = require('./../../sjg/kernel/obj/TextExtObj');
//var ImageObj = require('./../../sjg/kernel/obj/ImageObj');

var ItbUserModel = require('../../../common/mysql/mapping/ItbUserModel');
var TgSearchTagModel = require('../../../common/mysql/mapping/TgSearchTagModel');
var ItbNotificationModel = require('../../../common/mysql/mapping/ItbNotificationModel');
var TgTypeModel = require('../../../common/mysql/mapping/TgTypeModel');
var TgSpecialTopicModel = require('../../../common/mysql/mapping/TgSpecialTopicModel');
var TgKeywordModel = require('../../../common/mysql/mapping/TgKeywordModel');
var TgOutlineModel = require('../../../common/mysql/mapping/TgOutlineModel');
var TgDesignDocModel = require('../../../common/mysql/mapping/TgDesignDocModel');
var TgUserDocModel = require('../../../common/mysql/mapping/TgUserDocModel');
var TgUserPageModel = require('../../../common/mysql/mapping/TgUserPageModel');
var TgUserPcModel = require('../../../common/mysql/mapping/TgUserPcModel');
var TgDesignPcModel = require('../../../common/mysql/mapping/TgDesignPcModel');
var ItbBuyTmplModel = require('../../../common/mysql/mapping/ItbBuyTmplModel');
var ItbTmplPriceModel = require('../../../common/mysql/mapping/ItbTmplPriceModel');
var ItbTemplateModel = require('../../../common/mysql/mapping/ItbTemplateModel');
var ItbItemsModel = require('../../../common/mysql/mapping/ItbItemsModel');
var ItbDocTypeModel = require('../../../common/mysql/mapping/ItbDocTypeModel');
var ItbSceneModel = require('../../../common/mysql/mapping/ItbSceneModel');
var ItbMemberModel = require('../../../common/mysql/mapping/ItbMemberModel');
var ItbTmplPageModel = require('../../../common/mysql/mapping/ItbTmplPageModel');
var TgDesignPageModel = require('../../../common/mysql/mapping/TgDesignPageModel');
var ItbActivityRModel = require('../../../common/mysql/mapping/ItbActivityRModel');
var ItbActivityModel = require('../../../common/mysql/mapping/ItbActivityModel');


var CONFIG = require('../../../common/config');
var CODE = require('../../../common/code');
var payUtil = require('./payUtil');
var debugUtil = require('./debugUtil');
var CommonUtil = require('../../../common/util/CommonUtil');
var activityUtil = require('./activityUtil');
var printUtil = require('./printUtil');

var BASE_IP = CONFIG.BASE_IP;

var ParserClassLoader = require('./../../sjg/kernel/base/ParserClassLoader');
ParserClassLoader.loadKernelClass();// 加载类加载器

var tg3Util = (function () {
    function tg3Util() {
    }

    //------------------------------------------------------------------
    // 返回json
    tg3Util.ret_func_json = function (success, msg, data, res){
        var obj = {
            success: success,
            msg: msg,
            data: data
        };
        // 返回
        res.set('Content-Type', 'application/json');
        res.json(obj);
    }

    //------------------------------------------------------------------
    /**获取模板/作品提纲微调信息
     * 测试地址: http://localhost:3000/v5/tg3/fixTgDataOfV1
     *          http://localhost:3000/v5/tg3/fixTgDataOfV1
     * */
    tg3Util.fixTgDataOfV1 = function  (callback) {
        function fixTgDataOfV1Core(designModel, pcData){
            var isUpdate = 0;
            if(!designModel.data || !pcData || pcData.length==0 )    return isUpdate;

            designModel.data = CommonUtil.process_before_JSON_Parse(designModel.data);

            var tg = new SJGTG();
            tg.fromJson(JSON.parse(designModel.data),designModel.ver);

            for(var i=0;i<pcData.length;i++){
                var one = pcData[i];
                if(one){
                    var strFtData = CommonUtil.process_before_JSON_Parse(one.data);
                    var sjgPg =new SJGPage(new GlobalData());
                    sjgPg.fromJsonOfTG(JSON.parse(strFtData), one.ver);
                    if(sjgPg.replaces){
                        for(var j=0;j<sjgPg.replaces.length;j++){
                            var one = sjgPg.replaces[j];
                            if(one && one.id&& one.obj_id){
                                var f = tg.root.getNodeById(one.id);
                                if(f){
                                    if(!f.hasOwnProperty('obj_id') || !f.obj_id ) {
                                        f.obj_id = one.obj_id;
                                        isUpdate = 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if(isUpdate){
                if(tg.root.hasOwnProperty('h')){// v1部分数据有 h o p child_max
                    designModel.data = tg.toJson();
                }else{// v1部分数据无 h o p child_max
                    designModel.data = tg.toJson2();
                }

            }


            return isUpdate;
        }

        // 处理一个文档：
        function processOne(designModel, cb){
            if(designModel.ver!=1)   {
                return cb(null, 0);
            }


            var entity = new TgDesignPcModel({
                design_id:designModel.design_id,
                del_flg:0
            });

            //// test
            //if(designModel.design_id!=252){
            //    return cb(null, 0);
            //}

            entity.getPcDataOfDesign(function(error, pcData){
                if(!pcData || pcData.length==0){
                    return cb(error, null);
                }else{
                    // 生成微调数据
                    var ret = fixTgDataOfV1Core(designModel, pcData);
                    if(!ret){
                        console.log('模版'+designModel.design_id+'的提纲的obj_id未发生修改');
                        return cb(null,0);
                    }else{
                        // 更新到模版中
                        var updEnt = new TgDesignDocModel({
                            design_id: designModel.design_id,
                            data: designModel.data,
                        });
                        updEnt.updateToDB(function(error){
                            if(error){
                                return cb(error, 0);
                            }
                            return cb(null, 0);
                        });
                    }
                }
            });

        }
        // 总个数
        var entity = new TgDesignDocModel({
            ver:1,
            del_flg:0
        });
        entity.findCountFromDB(function(error, count){
            if (error) {
                console.log(error);
                return callback(error,0);
            }
            // 串行更新
            var asyncFuncs = [];
            for (var i=0;i<count;i++) {
                //if(i!=149)    continue;

                asyncFuncs.push((function (index) {
                    return function (asyncCallback) {  // asyncCallback(null, 'one');// asyncCallback(null, 'two');
                        var entity2 = new TgDesignDocModel({
                            ver:1,
                            del_flg:0
                        });
                        entity2.findFromDB(function(error,items) {
                            if (error) {
                                console.log(error);
                                return asyncCallback(error, 0);
                            }
                            // 处理单个文档
                            return processOne(items[0], asyncCallback);
                        },index,1);
                    };
                })(i)); // 利用闭包解决动态参数传递问题
            }
            async.series(asyncFuncs,function (err, results) {
                if(err){
                    return callback(err, 0);
                }
                return callback(null, count);
            });
        });
    }


    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：type:分类: 1单页, 2多页, 3ppt，可以为null或者0表示不限制分类
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取热门搜索关键词的数组
     *
     */
    tg3Util.get_hotsSearch = function (type, callback){
        var entity = new TgSearchTagModel({
            del_flg:0
        });
        if(type){
            entity.type = type;
        }

        var pageSize = 12;
        entity.findFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var hotsSearch = [];
            for(var i=0;i<items.length;i++){
                hotsSearch.push(items[i].name);
            }
            return callback(null, hotsSearch);
        },0,pageSize);
    }
    // todo 新增接口, 待审核 begin
    /* 获取首页内容信息
     * 参数：type:分类: 1单页, 2多页, 3ppt，可以为null或者0表示不限制分类
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:热门搜索词的数组
     *
     */
    tg3Util.get_hotsSearch_v5_2 = function(typeLs, callback) {
        var entity = new TgSearchTagModel({
            del_flg:0
        });

        var pTypeLs = '';
        if(typeLs && typeLs.length>0) {
            pTypeLs = typeLs.join(',');
        }
        entity.getHotsSearchByTypeLs(pTypeLs, function(error, items){
            if(error){
                console.log('tg3Util.js [get_hotsSearch_v5_2] error' + error);
                return callback(error, null);
            }

            var hotsSearch = [];
            for(var i=0;i<items.length;i++){
                hotsSearch.push(items[i].name);
            }
            return callback(null, hotsSearch);
        });

    }
    // todo 新增接口, 待审核 end

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取adInfo的数组，数组元素如下：
     *                 url:
     *                 thumb:
     *                 title:
     *
     */
    tg3Util.get_adInfo = function ( callback){
        var entity = new ItbNotificationModel({
            type:3,// 消息分类: 1:普通msg; 2:横幅banner信息；3:大幅广告
            del_flg:0
        });

        var pageSize = 12;
        entity.getMessage(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    url:items[i].url,
                    thumb:items[i].image,
                    title:items[i].title
                });
            }
            return callback(null, adInfo);
        });

    }

    //------------------------------------------------------------------
    /* 获取首页轮播信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取adInfo的数组，数组元素如下：
     *                 kind: 轮播图类型(新增): 1.纯展示 2.点击可跳转url 3.点击跳转至专题列表
     *                 thumb: 轮播图对应的图片
     *                 url: 展示链接
     *                 title: 展示 url 时显示的标题
     *                 id: 展示专题时对应的专题 id(新增)
     *                 typeL: 业务分类(新增)
     *
     */
    tg3Util.get_adInfo_by_type = function (type, callback){
        var THIS = this;
        var entity = new ItbNotificationModel({
            type: type,// 消息分类: 1:App普通msg; 2:App横幅banner信息；3:App轮播图; 13:Web轮播图
            del_flg:0
        });
        var pageSize = 12;
        entity.getMessage(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    kind: (items[i].url && items[i].url.length>0) ? 2 : 1,
                    thumb: items[i].image||'',
                    url: items[i].url||'',
                    title: items[i].title||'',
                    id: 0,
                    type: 0,
                    typeL: 0
                });
            }

            return callback(null, adInfo);
        });
    }

    //------------------------------------------------------------------
    /* 获取首页轮播信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取adInfo的数组，数组元素如下：
     *                 kind: 轮播图类型(新增): 1.纯展示 2.点击可跳转url 3.点击跳转至专题列表
     *                 thumb: 轮播图对应的图片
     *                 url: 展示链接
     *                 title: 展示 url 时显示的标题
     *                 id: 展示专题时对应的专题 id(新增)
     *                 typeL: 业务分类(新增)
     *
     * 参数: withOutTopic: true不带专题
     */
    tg3Util.get_adInfo_v5_1 = function (callback, withoutTopic){
        var THIS = this;
        var entity = new ItbNotificationModel({
            type:3,// 消息分类: 1:普通msg; 2:横幅banner信息；3:大幅广告
            del_flg:0
        });
        var pageSize = 12;
        entity.getMessage(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    //kind: (items[i].url && items[i].url.length>0) ? 2 : 1,
                    kind: (items[i].url && items[i].url.length>0 ) ? (items[i].encrypt == 0 ? 2 : 4 ) : 1,
                    thumb: items[i].image||'',
                    url: items[i].url||'',
                    title: items[i].title||'',
                    id: 0,
                    type: 0,
                    typeL: 0
                });
            }

            if(withoutTopic) {
                return callback(null, adInfo);
            }

            // 获取专题
            THIS.get_hotsProject_core(2,null,function(err,data){
                if(err) {
                    console.log(err);
                    //return callback(err);
                }

                for(var j=0;j<data.length;j++){
                    adInfo.push({
                        kind: 3,
                        thumb: data[j].thumb||'',
                        url: '',
                        title: data[j].title||'',
                        id: data[j].id,
                        type: data[j].type,
                        typeL: data[j].typeL,
                    });
                }
                return callback(null, adInfo);
            });
        });
    }

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：use: 用途: 1热门, 2专题
     * 参数：type:分类: 0未知 1单页, 2多页, 3ppt,可以传入null
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsProject的数组，数组元素如下：
     *                 id:
     *                 thumb:
     *                 title:
     *                 desc:
     *                 typeL
     *
     */
    tg3Util.get_hotsProject_core = function ( use,type,callback){
        var entity = new TgSpecialTopicModel({
            use:use,    // 用途: 1热门, 2专题
            publish:1,  // 已发布
            del_flg:0
        });
        if(type){
            entity.type = type;// 分类: 0未知 1单页, 2多页, 3ppt
        }

        var pageSize = 12;
        entity.findSpecTopicWithType(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    id:items[i].st_id,
                    thumb:items[i].thumb,
                    title:items[i].title,
                    desc:items[i].desc,
                    type:items[i].type,
                    typeL:items[i].top_type
                });
            }
            return callback(null, adInfo);
        },0,pageSize);
    }

    //------------------------------------------------------------------
    /* 获取专题的内容列表
     * 参数：stId：专题id
     * 参数：pageNum：页码
     * 参数：pageSize：
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取专题的内容列表的数组，数组元素如下：
     *                 type:
     *                 id: 模版id(印品时返回商品id)
     *                 thumb:
     *                 name:
     */
    tg3Util.getSpecialTopicItems = function ( stId,pageNum,pageSize,callback){
        var entity = new TgSpecialTopicModel({
            st_id:stId
        });


        var pageSize = pageSize;
        var skipNum = pageNum*pageSize;
        entity.findRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                if(Number(items[i].type_id||'0')==4) {
                    adInfo.push({
                        type: 4,
                        id: items[i].goods_id,
                        thumb: items[i].goods_thumb,
                        name: items[i].goods_name,
                        w:CONFIG.PRINT_ITEM_PREVIEW_WIDTH,
                        h:CONFIG.PRINT_ITEM_PREVIEW_HEIGHT,
                        // todo 模拟数据, 待实现 begin
                        typeL : 4,
                        title : items[i].goods_name,
                        desc : items[i].goods_desc,
                        useCount : items[i].show_apply_cnt||items[i].apply_cnt||0
                        // todo 模拟数据, 待实现 end
                    });
                } else {
                    adInfo.push({
                        type: items[i].type_id,
                        id: items[i].tmpl_id,
                        thumb: items[i].thumb,
                        name: items[i].tmpl_name,
                        w:items[i].width,
                        h:items[i].height,
                        // todo 模拟数据, 待实现 begin
                        typeL : items[i].type_id,
                        title : items[i].tmpl_name,
                        desc : items[i].goods_desc,
                        useCount : items[i].show_apply_cnt||items[i].apply_cnt||0
                        // todo 模拟数据, 待实现 end
                    });
                }
            }
            return callback(null, adInfo);
        },skipNum,pageSize);
    }

    //------------------------------------------------------------------
    /* 获取专题的内容列表
     * 参数：stId：专题id
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          column:列数
     */
    tg3Util.getSpecialTopicColumn  = function ( stId,callback) {
        var entity = new TgSpecialTopicModel({
            st_id: stId,
            del_flg: 0
        });
        var pageSize = 1;
        entity.findFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error,null);
            }
            if(!items || items.length==0){
                return callback("数据不存在!",null)
            }
            var type = items[0].type;
            var column = tg3Util.getAppShowColumn(type,type,type);
            return callback(null, column);
        },0,pageSize);
    }
    //------------------------------------------------------------------
    /* 获取专题的内容列表
     * 参数：type：1单页, 2多页, 3ppt
     * 参数：pageNum：页码
     * 参数：pageSize：
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取专题的内容列表的数组，数组元素如下：
     *                 id: 模板id
     *                 thumb:
     *                 typeL:
     *                 title: 模板名称
     *                 desc: 价格描述
     *                 w:
     *                 h:
     *
     */
    tg3Util.getHotTopicItems = function ( type,pageNum,pageSize,callback){
        var entity = new TgSpecialTopicModel({
        });

        var skipNum = pageNum*pageSize;
        entity.findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    id:items[i].item_id,
                    thumb:items[i].thumb,
                    typeL: type,
                    title: items[i].tmpl_name||'',
                    desc: items[i].tmpl_price||'',
                    w:items[i].width,
                    h:items[i].height,
                    useCount: items[i].show_apply_cnt||items[i].apply_cnt||0
                });
            }
            return callback(null, adInfo);
        },type,skipNum,pageSize);
    };

    //------------------------------------------------------------------
    /* 获取印品专题的内容列表
     * 参数：type：印品的二级分类
     * 参数：pageNum：页码
     * 参数：pageSize：
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取专题的内容列表的数组，数组元素如下：
     *                 id: 模板id
     *                 thumb:
     *                 typeL:
     *                 title: 模板名称
     *                 desc: 价格描述
     *                 w:
     *                 h:
     *
     */
    tg3Util.getHotTopicItemsOfPrintTypeM = function ( type,pageNum,pageSize,callback){
        var entity = new TgSpecialTopicModel({
        });

        var skipNum = pageNum*pageSize;
        entity.findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    id: items[i].goods_id,
                    thumb: items[i].thumb||'',
                    typeL: 4,// 印品
                    title: items[i].goods_name||'',
                    desc: items[i].goods_price||'',
                    w: CONFIG.PRINT_ITEM_PREVIEW_WIDTH,
                    h: CONFIG.PRINT_ITEM_PREVIEW_HEIGHT,
                    useCount: items[i].show_apply_cnt||items[i].apply_cnt||0
                });
            }
            return callback(null, adInfo);
        },type,skipNum,pageSize);
    };

    /* 获取社交媒体的热门列表
     * 参数：type：社交媒体的二级分类
     * 参数：pageNum：页码
     * 参数：pageSize：
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取专题的内容列表的数组，数组元素如下：
     *                 id: 模板id
     *                 thumb:
     *                 typeL:
     *                 title: 模板名称
     *                 desc: 价格描述
     *                 w:
     *                 h:
     *
     */
    tg3Util.getHotTopicItemsOfSocialMediaTypeM = function(type,pageNum,pageSize,callback) {
        var entity = new TgSpecialTopicModel({
        });

        var skipNum = pageNum*pageSize;
        entity.findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    id:items[i].item_id,
                    thumb:items[i].thumb,
                    typeL: 6, // 社交媒体
                    title: items[i].tmpl_name||'',
                    desc: items[i].tmpl_price||'',
                    w:items[i].width,
                    h:items[i].height,
                    useCount: items[i].show_apply_cnt||items[i].apply_cnt||0
                });
            }
            return callback(null, adInfo);
        },type,skipNum,pageSize);
    };

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsProject的数组，数组元素如下：
     *                 id:
     *                 thumb:
     *                 title:
     *                 desc:
     *                 typeL:
     */
    tg3Util.get_hotsProject = function ( callback){
        var use=2; // 用途: 1热门, 2专题
        var type=null;// 分类: 0未知 1单页, 2多页, 3ppt,可以传入null
        this.get_hotsProject_core(use,type,function(error,data){
            return callback(error,data);
        });
    }
    //------------------------------------------------------------------
    /* 获取首页热门ppt内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsPPT的数组，数组元素如下：
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     */
    tg3Util.get_hotsPPT = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=3;// 分类: 1单页, 2多页, 3ppt
        this.getHotTopicItems(type,pageNum, pageSize,function(error,data){
            return callback(error,data);
        });
    }
    /* 获取首页热门ppt内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsPPT
     *          {
     *              title:"热门幻灯片"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }

     */
    tg3Util.get_hotsPPT_v5_3 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=3;// 分类: 1单页, 2多页, 3ppt
        this.getHotTopicItems(type,pageNum, pageSize,function(error,data){
            // 热门与列表展示列数不同
            data = {
                title: "热门幻灯片",
                en: CONFIG.CHS_EN.getEn('热门幻灯片',1),
                typeL: 3,
                typeM: -1,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    }
    //------------------------------------------------------------------
    /* 获取首页热门海报内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsPoster的数组，数组元素如下：
     *                 id:
     *                 thumb:
     *                 title:
     *                 desc:
     *                 typeL
     *
     */
    tg3Util.get_hotsPoster = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=1;// 分类: 1单页, 2多页, 3ppt
        this.getHotTopicItems(type,pageNum, pageSize,function(error,data){
            return callback(error,data);
        });
    };

    /* 获取首页热门海报内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsPoster下：
     *          {
     *              title:"热门海报"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     *
     */
    tg3Util.get_hotsPoster_v5_3 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=1;// 分类: 1单页, 2多页, 3ppt
        this.getHotTopicItems(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "热门海报",
                en: CONFIG.CHS_EN.getEn('热门海报',1),
                typeL: 1,
                typeM: -1,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 3.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    };

    //------------------------------------------------------------------
    /* 获取首页热门公众号封面大图内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:返回如下：
     *          {
     *              title:"公众号封面大图"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     *
     */
    tg3Util.get_hotsWechatCoverBigPic_v5_3 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=60100;// (社交媒体/公众号封面大图)
        this.getHotTopicItemsOfSocialMediaTypeM(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "热门公众号首图",
                en: CONFIG.CHS_EN.getEn('热门公众号首图',1),
                typeL: 6,
                typeM: 60100,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    };

    //------------------------------------------------------------------
    /* 获取首页热门公众号封面小图内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:返回如下：
     *          {
     *              title:"热门公众号封面小图"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     *
     */
    tg3Util.get_hotsWechatCoverSmallPic_v5_3 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=60200;// (社交媒体/公众号封面小图)
        this.getHotTopicItemsOfSocialMediaTypeM(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "热门公众号小图",
                en: CONFIG.CHS_EN.getEn('热门公众号小图',1),
                typeL: 6,
                typeM: 60200,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 3.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    };


    //------------------------------------------------------------------
    /* 获取首页热门微博封面图内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:返回如下：
     *          {
     *              title:"热门微博封面图"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     *
     */
    tg3Util.get_hotsWeiboPic_v5_3  = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=60300;// (社交媒体/微博封面图)
        this.getHotTopicItemsOfSocialMediaTypeM(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "热门微博封面图",
                en: CONFIG.CHS_EN.getEn('热门微博封面图',1),
                typeL: 6,
                typeM: 60300,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 1.5,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    };

    //------------------------------------------------------------------
    /* 获取首页热门多页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsMultiPage的数组，数组元素如下：
     *                 id:
     *                 thumb:
     *                 title:
     *                 desc:
     *                 typeL
     *
     */
    tg3Util.get_hotsMultiPage = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=2;// 分类: 1单页, 2多页, 3ppt
        this.getHotTopicItems(type,pageNum, pageSize,function(error,data){
            return callback(error,data);
        });
    };
    //------------------------------------------------------------------
    /* 获取热门名片信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsBusinessCard的数组，数组元素如下：
     *                 id: 商品id
     *                 thumb:
     *                 typeL:
     *                 title: 商品名
     *                 desc: 价格描述
     */
    tg3Util.get_hotsBusinessCard = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40100;
        var skipNum = pageNum*pageSize;
        (new TgSpecialTopicModel).findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            var retInfo = [];
            for(var i=0;i<items.length;i++){
                retInfo.push({
                    id: items[i].goods_id,
                    thumb: items[i].thumb||'',
                    typeL: 4,// 印品
                    title: items[i].goods_name||'',
                    desc: items[i].goods_price||''
                });
            }
            return callback(null, retInfo);
        },type,skipNum,pageSize);
    };

    /* 获取热门名片信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsBusinessCard：
     *          {
     *              title:"热门名片"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     */
    tg3Util.get_hotsBusinessCard_v5_3 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40100;

        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            // 热门与列表显示列数不同
            var data = {
                title: "热门名片",
                en: CONFIG.CHS_EN.getEn('热门名片',1),
                typeL: 4,
                typeM: 40100,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    //------------------------------------------------------------------
    /* 获取热门传单信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsLeaflet的数组，数组元素如下：
     *                 id: 商品id
     *                 thumb:
     *                 typeL:
     *                 title: 商品名
     *                 desc: 价格描述
     */
    tg3Util.get_hotsLeaflet = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40200;
        var skipNum = pageNum*pageSize;
        (new TgSpecialTopicModel).findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            var retInfo = [];
            for(var i=0;i<items.length;i++){
                retInfo.push({
                    id: items[i].goods_id,
                    thumb: items[i].thumb||'',
                    typeL: 4,// 印品
                    title: items[i].goods_name||'',
                    desc: items[i].goods_price||''
                });
            }
            return callback(null, retInfo);
        },type,skipNum,pageSize);
    };

    /* 获取热门传单信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsLeaflet：
     *          {
     *              title:"热门传单"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     */
    tg3Util.get_hotsLeaflet_v5_3 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40200;

        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            // 热门与列表显示列数不同
            var data = {
                title: "热门传单",
                en: CONFIG.CHS_EN.getEn('热门传单',1),
                typeL: 4,
                typeM: 40200,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    //------------------------------------------------------------------
    /* 获取热门展架数据
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsDisplayRack的数组，数组元素如下：
     *                 id: 商品id
     *                 thumb:
     *                 typeL:
     *                 title: 商品名
     *                 desc: 价格描述
     */
    // todo 新方法, 待审核 begin
    tg3Util.get_hotsDisplayRack_v5 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40300; // 展架用图
        var skipNum = pageNum*pageSize;
        (new TgSpecialTopicModel).findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log('tg3Util.js [get_hotsDisplayRack_v5] error: '+error);
                return callback(error, null);
            }
            var retInfo = [];
            for(var i=0;i<items.length;i++){
                retInfo.push({
                    id: items[i].goods_id,
                    thumb: items[i].thumb||'',
                    typeL: 4,// 印品
                    title: items[i].goods_name||'',
                    desc: items[i].goods_price||''
                });
            }
            return callback(null, retInfo);
        },type,skipNum,pageSize);
    };

    /* 获取热门展架数据
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取hotsDisplayRack：
     *          {
     *              title:"热门展架"
     *              column: 2,
     *              list:[
     *                 id:
     *                 thumb:
     *                 title:
     *                 typeL
     *                 desc:
     *                 w:
     *                 h:
     *              ]
     *          }
     */
    // todo 新方法, 待审核 begin
    tg3Util.get_hotsDisplayRack_v5_3 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=40300; // 展架用图

        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            // 热门与列表显示列数不同
            var data = {
                title: "热门展架",
                en: CONFIG.CHS_EN.getEn('热门展架',1),
                typeL: 4,
                typeM: 40300,
                //column:tg3Util.getAppShowColumn(type,type,type),
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：typeL: 分类: 1单页, 2多页
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取TypeM的数组，数组元素如下：
     *              id
     *              name
     *              icon(v5_1新增)
     */
    tg3Util.get_child_type_core = function (typeL,  callback){
        //`state` int(2) NOT NULL DEFAULT '1' COMMENT '1新建,2可制作模版,3可对外发布',
        var entity = new TgTypeModel({
            parent: typeL,
            state:3,
            del_flg:0
        });

        var pageSize = 12;
        entity.getTypeInfoByParent(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var ret = [];
            if(items ){
                for(var i=0;i<items.length;i++){
                    ret.push({
                        id: items[i].type_id,
                        name: items[i].type_name||'',
                        en: CONFIG.CHS_EN.getEn(items[i].type_name||'',1),// added 2017-06-02
                        icon: items[i].icon||''
                    });
                }
            }

            return callback(null, ret);
        },0,pageSize);
    };

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取poseterTypeM
     *
     */
    tg3Util.get_poseterTypeM = function ( callback){
        var typeL = 1;//分类: 1单页, 2多页;
        return this.get_child_type_core(typeL,callback);
    };
    tg3Util.get_poseterTypeM_v5_1 = function ( callback){
        var typeL = 1;//分类: 1单页, 2多页;
        return this.get_child_type_core(typeL,function(err,data){
            var newItem = {
                id: -1,
                name: '最新',
                en: 'NEW',
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_all.png',
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821250_piyCcowbMXmxe2NrFWqX',
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/145834596_ZrEsDWtDHCjvUnEcU3Ax'// v5.5
            };

            if(data && data.length>0) {
                data.splice(0,0,newItem);
            } else {
                data = [newItem];
            }

            callback(err,data);
        });
    };
    //------------------------------------------------------------------
    // 社交媒体
    tg3Util.get_socialMediaTypeM_v5_3  = function ( callback){
        var typeL = 6;//分类: 1单页, 2多页, 6社交媒体;

        var data = [
            {
                id : 60100,
                name : "公众号首图",
                en: CONFIG.CHS_EN.getEn('公众号首图',1),
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/21/151413667_aT280OgpD1RqvsPOivyJ'
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/144623714_p2sdxCRdoOvCpVaJF1pt'// v5.5
            },
            {
                id : 60200,
                name : "公众号小图",
                en: CONFIG.CHS_EN.getEn('公众号小图',1),
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/21/151413491_CRm6WGpMeeQ51rif2kk1'
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/144623570_edPABNtm44VyV0LAn3Rd'// v5.5
            }
        ];
        return callback(null,data);
    };

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取multiPageTypeM
     *
     */
    tg3Util.get_multiPageTypeM = function ( callback){
        var typeL = 2;//分类: 1单页, 2多页;
        return this.get_child_type_core(typeL,callback);
    };

    //------------------------------------------------------------------
    /* 获取首页内容信息
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取printTypeM
     *
     */
    // todo 新增方法, 待审核 begin
    tg3Util.get_printTypeM_v5_2 = function ( callback){

        var printTypeMs = [
            {"id":40100, "name":"名片", "en":CONFIG.CHS_EN.getEn('名片',1), "icon":""},
            {"id":40200, "name":"传单", "en":CONFIG.CHS_EN.getEn('传单',1), "icon":""},
            {"id":40300, "name":"展架", "en":CONFIG.CHS_EN.getEn('展架',1), "icon":""}
        ];
        return callback(null, printTypeMs);

        //var typeL = 4;//分类: 1单页, 2多页, 3 ppt, 4 印品;
        //return this.get_child_type_core(typeL,callback);
    };
    // todo 新增方法, 待审核 end

    tg3Util.get_printTypeM_v5_5 = function ( callback){

        var printTypeMs = [
            {"id":40100, "name":"名片", "en":CONFIG.CHS_EN.getEn('名片',1), "icon":""},
            {"id":40200, "name":"传单", "en":CONFIG.CHS_EN.getEn('传单',1), "icon":""},
            {"id":40300, "name":"展架", "en":CONFIG.CHS_EN.getEn('展架',1), "icon":""},
            {"id":40500, "name":"招贴海报", "en":CONFIG.CHS_EN.getEn('招贴海报',1), "icon":""}
        ];
        return callback(null, printTypeMs);

        //var typeL = 4;//分类: 1单页, 2多页, 3 ppt, 4 印品;
        //return this.get_child_type_core(typeL,callback);
    };

    tg3Util.get_printTypeM_v5_6 = function ( callback){

        var printTypeMs = [
            {"id":40100, "name":"名片", "en":CONFIG.CHS_EN.getEn('名片',1), "icon":""},
            {"id":40200, "name":"传单", "en":CONFIG.CHS_EN.getEn('传单',1), "icon":""},
            {"id":40300, "name":"展架", "en":CONFIG.CHS_EN.getEn('展架',1), "icon":""},
            {"id":40500, "name":"招贴海报", "en":CONFIG.CHS_EN.getEn('招贴海报',1), "icon":""},
            {"id":40600, "name":"优惠券", "en":CONFIG.CHS_EN.getEn('优惠券',1), "icon":""},
            {"id":40700, "name":"不干胶", "en":CONFIG.CHS_EN.getEn('不干胶',1), "icon":""},
        ];
        return callback(null, printTypeMs);

        //var typeL = 4;//分类: 1单页, 2多页, 3 ppt, 4 印品;
        //return this.get_child_type_core(typeL,callback);
    };
    tg3Util.get_printTypeM_v5_7 = function ( callback){

        var printTypeMs = [
            {"id":40100, "name":"名片", "en":CONFIG.CHS_EN.getEn('名片',1), "icon":""},
            {"id":40200, "name":"传单", "en":CONFIG.CHS_EN.getEn('传单',1), "icon":""},
            {"id":40300, "name":"展架", "en":CONFIG.CHS_EN.getEn('展架',1), "icon":""},
            {"id":40500, "name":"海报", "en":CONFIG.CHS_EN.getEn('海报',1), "icon":""},
            {"id":40600, "name":"优惠券", "en":CONFIG.CHS_EN.getEn('优惠券',1), "icon":""},
            {"id":40700, "name":"不干胶", "en":CONFIG.CHS_EN.getEn('不干胶',1), "icon":""},
            {"id":40800, "name":"停车牌", "en":CONFIG.CHS_EN.getEn('停车牌',1), "icon":""},
        ];
        return callback(null, printTypeMs);

        //var typeL = 4;//分类: 1单页, 2多页, 3 ppt, 4 印品;
        //return this.get_child_type_core(typeL,callback);
    };

    //------------------------------------------------------------------
    /*＊
     * 获取ppt分类
     * 参数 category(相当于typeM): 分类: 1行业; 2场景
     */
    tg3Util.get_pptType_v5_5 = function(category,callback){
        (new ItbDocTypeModel).getTypeByCat(category, function(err, ret){
            if(err) {
                return callback(err, null);
            }

            var retData = [];
            retData.push({
                id: -1,
                name: '最新',
                en: 'NEW',
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/120527104_zdnJEbTb5ahTE5FooZIb',
            });

            if(ret && ret.length>0) {
                for(var i=0; i<ret.length; i++) {
                    retData.push({
                        id: ret[i].type_id,// 相当于typeS
                        name: ret[i].type_name,
                        en: CONFIG.CHS_EN.getEn(ret[i].type_name,1),
                        icon: ret[i].icon,
                    });
                }
            }
            return callback(null, retData);
        });
    };

    //------------------------------------------------------------------
    /* 首页底部提示数据
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:{title: '提示信息主体', desc: '提示附加信息'}
     *
     */
    tg3Util.get_bottomInfo = function(callback){
        var retData = {
            "title" : "网页版已经上线, 制作更加轻松快捷~",
            "desc" : "www.itbour.com"
        }
        return callback(null, retData);
    }

    //------------------------------------------------------------------
    /* 获取设计列表数据(过滤vip)
     * 参数: typeL	请求数据的一级分类
     * 参数: typeM	请求数据的二级分类(-1 标示最新)
     * 参数[可选]: typeS	请求数据的三级分类, 若不存在标识仅以一级二级分类请求数据
     * 参数: pageNum	请求指定页页码
     * 参数: pageSize	请求指定页大小
     * 返回值:
         "list":[
             {
                 "id":387,
                 "thumb":"http://xxxx.jpg",
                 "title":"商务办公海报特惠",
                 "desc":"1.90 元",
                 "typeL":4
             },
             ...
         ]
     */
    tg3Util.getEquivalentTypeList = function(typeL,typeM,typeS,pageNum,pageSize,callback){
        // 分页: 跳过条数 
        var skipCnt = Number(pageNum||'0') * Number(pageSize||'0');

        if(typeL==1 || typeL==2 || typeL==6 || typeL==7 || typeL == 8){
            // 海报/多页
            (new TgDesignDocModel).getDesignInfoByType(typeL,typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    if(Number(ret[i].vip||'0')==0) {
                        retData.push({
                            id:ret[i].design_id,
                            thumb:ret[i].thumb||'',
                            title:ret[i].design_name||'',
                            desc:ret[i].tmpl_price||'',
                            typeL:typeL,
                            w:ret[i].width,
                            h:ret[i].height,
                            useCount:ret[i].show_apply_cnt||ret[i].apply_cnt||0
                        });
                    }
                }
                return callback(null,retData);
            },true/*过滤vip模版*/);

        } else if(typeL==3) {
            // PPT
            (new ItbTemplateModel).getPptInfoByType(typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    if(Number(ret[i].vip||'0')==0) {// 过滤vip模版
                        retData.push({
                            id: ret[i].tmpl_id,
                            thumb: ret[i].thumb || '',
                            title: ret[i].tmpl_name || '',
                            desc: ret[i].tmpl_price || '',
                            typeL: 3,
                            w: 1600,
                            h: 900,
                            useCount: ret[i].show_apply_cnt || ret[i].apply_cnt || 0
                        });
                    }
                }
                return callback(null,retData);
            },true/*过滤vip模版*/);

        } else if(typeL==4) {
            // 印品
            (new ItbItemsModel).getItemInfoByType(typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    if(Number(ret[i].vip||'0')==0) {// 过滤vip模版
                        retData.push({
                            id: ret[i].goods_id,
                            thumb: ret[i].thumb || '',
                            title: ret[i].goods_name || '',
                            desc: ret[i].goods_price || '',
                            typeL: 4,
                            w: ret[i].width,
                            h: ret[i].height,
                            useCount: ret[i].show_apply_cnt || ret[i].apply_cnt || 0
                        });
                    }
                }
                return callback(null,retData);
            },true/*过滤vip模版*/);

        } else {
            callback(null, null);
        }
    };

    // 添加vip返回字段
    tg3Util.getEquivalentTypeList_v6_0 = function(typeL,typeM,typeS,pageNum,pageSize,callback){
        // 分页: 跳过条数 
        var skipCnt = Number(pageNum||'0') * Number(pageSize||'0');

        if(typeL==1 || typeL==2 || typeL==6 || typeL==7 || typeL==8){
            // 海报/多页/社交媒体/电商/微商
            (new TgDesignDocModel).getDesignInfoByType(typeL,typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    retData.push({
                        id:ret[i].design_id,
                        thumb:ret[i].thumb||'',
                        title:ret[i].design_name||'',
                        desc:ret[i].tmpl_price||'',
                        typeL:typeL,
                        w:ret[i].width,
                        h:ret[i].height,
                        useCount:ret[i].show_apply_cnt||ret[i].apply_cnt||0,
                        vip:ret[i].vip||0
                    });
                }
                return callback(null,retData);
            });

        } else if(typeL==3) {
            // PPT
            (new ItbTemplateModel).getPptInfoByType(typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    retData.push({
                        id:ret[i].tmpl_id,
                        thumb:ret[i].thumb||'',
                        title:ret[i].tmpl_name||'',
                        desc:ret[i].tmpl_price||'',
                        typeL:3,
                        w:1600,
                        h:900,
                        useCount:ret[i].show_apply_cnt||ret[i].apply_cnt||0,
                        vip:ret[i].vip||0
                    });
                }
                return callback(null,retData);
            });

        } else if(typeL==4) {
            // 印品
            (new ItbItemsModel).getItemInfoByType(typeM,typeS,skipCnt,pageSize,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    retData.push({
                        id:ret[i].goods_id,
                        thumb:ret[i].thumb||'',
                        title:ret[i].goods_name||'',
                        desc:ret[i].goods_price||'',
                        typeL:4,
                        w:ret[i].width,
                        h:ret[i].height,
                        useCount:ret[i].show_apply_cnt||ret[i].apply_cnt||0,
                        vip:ret[i].vip||0
                    });
                }
                return callback(null,retData);
            });
        } else {
            callback(null, null);
        }
    };

    //------------------------------------------------------------------
    /* 获取app一行显示的列数
     * 参数: typeL	请求数据的一级分类  1 海报/3 ppt/4 印品/6社交媒体
     * 参数: typeM	请求数据的二级分类(-1 标示最新)
     * 参数[可选]: typeS	请求数据的三级分类, 若不存在标识仅以一级二级分类请求数据
     * 返回值: 列数
     */
    tg3Util.MAP_TYPEL_APP_SHOW_COLUMN =  { "1":3,"3":2,"4":2,"6":2, "8" : 3};
    tg3Util.MAP_TYPEM_APP_SHOW_COLUMN =  { "60100":1,"60200":2,"60300":1, "70100" : 2, "70200" : 1};
    tg3Util.MAP_TYPES_APP_SHOW_COLUMN =  {};
    tg3Util.getAppShowColumn = function(typeL, typeM, typeS){

        return tg3Util.MAP_TYPES_APP_SHOW_COLUMN[typeS]
            || tg3Util.MAP_TYPEM_APP_SHOW_COLUMN[typeM]
            || tg3Util.MAP_TYPEL_APP_SHOW_COLUMN[typeL]
            || 2;
    }

    //------------------------------------------------------------------
    /* 根据关键词搜索对应模板，多个单词以半角都好分隔
     * 参数 keyword:  关键词(去特殊字符, 以 空格分割, 以 半角 , 拼接)
     * 参数 typeL:  搜索分类,  0 未分类   1 海报  3 ppt  4 印品
     * 参数 pageNum: 搜索结果对应页
     * 参数 pageSize: 搜索结果请求页大小
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:结果数组， 数组元素如下：
     *              "thumb":"http://xxx.jpg", // 如typeL为4则id为印品商品图片,否则为模版图片
     *              "id":788, // 如typeL为4则id为印品商品id,否则为模版id
     *              "typeL":1,
     *              "desc":"幻灯片",
     *              "author":"by乌头马角工作室"
     *
     */
    tg3Util.get_search = function (keyword, typeL,pageNum, pageSize, callback){
        // 分词处理
        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 搜索
        var entity = new TgKeywordModel();
        entity.search(segKeyword, typeL, pageNum, pageSize, callback);
    }

    //------------------------------------------------------------------
    /* 根据关键词搜索对应模板，多个单词以半角都好分隔
     * 参数 keyword:  关键词(去特殊字符, 以 空格分割, 以 半角 , 拼接)
     * 参数 typeLs:   搜索分类,  0 未分类, 1 海报, 2 多页, 3 ppt, 4 印品 的数组的 json 字符串 如: “[1,3]”
     * 参数 pageNum:  搜索结果对应页
     * 参数 pageSize: 搜索结果请求页大小
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:结果数组， 数组元素如下：
     *              "thumb": "http://xxx.jpg", // 如typeL为4则id为印品商品图片,否则为模版图片
     *              "id": 788, // 如typeL为4则id为印品商品id,否则为模版id
     *              "typeL": 1,
     *              "desc": "幻灯片",
     *              "author": "by乌头马角工作室",
     *              "time": "2016-12-05 13:38"
     */
    tg3Util.get_search_v5_1 = function (keyword, typeLs, pageNum, pageSize, callback){
        // 分词处理
        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 拼接typeL
        var arrTypeL = JSON.parse(typeLs||'[]');
        var pTypeLs = '';
        if(arrTypeL && arrTypeL.length>0) {
            pTypeLs = arrTypeL.join(',');
        }

        // 搜索
        var entity = new TgKeywordModel();
        entity.search_v5_1(segKeyword, pTypeLs, pageNum, pageSize, callback);
    }

    // todo 修改接口, 已完成, 待审核 begin
    tg3Util.get_search_v5_1_Cnt = function (keyword, typeLs, callback) {
        var entity = new TgKeywordModel();

        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 拼接typeL
        var arrTypeL = JSON.parse(typeLs||'[]');
        var pTypeLs = '';
        if(arrTypeL && arrTypeL.length>0) {
            pTypeLs = arrTypeL.join(',');
        }
        entity.searchCnt_v5_1(segKeyword, pTypeLs, callback);
    }
    // todo 修改接口, 已完成, 待审核 end
    //------------------------------------------------------------------
    /* 根据顶级分类获取对应标签
     * 参数 typeL:  1 海报  2 多页
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:子节点数组，数组元素如下：
     *              "id":788,
     *              "name":"邀请函",
     *              "sub":[{
     *                  "id":788,
     *                  "name":"展会",
     *              }]
     */
    tg3Util.getTagByTypeL = function ( typeL, callback){
        var THIS = this;
        THIS.get_child_type_core(typeL,function(error, data){
            if(error){
                return callback(error, null);
            }

            var asyncFuncs = [];
            for(var i=0;i<data.length; i++){
                asyncFuncs.push((function (index, one) {
                    return function (asyncCallback) {
                        THIS.get_child_type_core(one.id,function(error, data) {
                            if (error) {
                                return asyncCallback(error, null);
                            }
                            one.sub = data;
                            asyncCallback(null, null);
                        });
                    }
                })(i, data[i])); // 利用闭包解决动态参数传递问题
            }
            async.parallel(asyncFuncs, function (error, results) {
                if (error) {
                    console.log('tg3Util.js [tg3Util.getTagByTypeL] error: '+error);
                    return callback(error, null);
                }

                return callback(null, data);
            });
        });
    }

    //------------------------------------------------------------------
    /* 根据指定标签数组获取对应条目数据
     * 参数 typeS:  三级分类
     * 返回:
     *{
     *    "success":0,
     *    "msg":"成功",
     *    "data":{
     *      "tgId":758,
     *      "tree":{"version":3,"name":"会议邀请函","root":{}}
     *    }
     *}
     * */
    tg3Util.getItemByTypeS = function ( typeS, callback){

        var entity = new TgOutlineModel({
            type_id:typeS,
            fast:1, // 每个三级分类下有一个题纲供三级分类使用
            del_flg:0
        });

        var pageSize = 1;
        entity.findFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error,null);
            }
            if(!items || items.length==0){
                return callback("数据不存在!",null)
            }

            var ret = {
                tgId:items[0].tg_id,
                tree:JSON.parse(items[0].data)
            };

            return callback(null, ret);
        },0,pageSize);

    }

    //------------------------------------------------------------------
    /* 根据指定TYPE检索fast提纲信息
     * 参数 typeS:  三级分类
     * 返回:
     *{
     *    "data":{
     *      "typeL":1
     *      "tgId":758,
     *      "tree":{"version":3,"name":"会议邀请函","root":{}}
     *    }
     *}
     * */
    tg3Util.getFastOutlineByType = function(typeL,typeM,typeS, callback){

        (new TgOutlineModel).getFastOutlineByType(typeL,typeM,typeS,function(error,items) {
            if (error) {
                console.log(error);
                return callback(error,null);
            }
            if(!items || items.length==0){
                return callback("数据不存在!",null)
            }

            var ret = {
                typeL:items[0].top_parent,
                tgId:items[0].tg_id,
                tree:JSON.parse(items[0].data)
            };

            return callback(null, ret);
        });
    }

    //------------------------------------------------------------------
    /* 获取题纲的数据
     * 参数 tgId:  条目选择获取的 tgId
     * 返回:
     *{
     *    "success":0,
     *    "msg":"成功",
     *    "data":{
     *      "tgId":758,
     *      "tree":{"version":3,"name":"会议邀请函","root":{}}
     *    }
     *}
     * */
    tg3Util.getTgTreeById = function ( tgId, callback){
        var entity = new TgOutlineModel({
            tg_id:tgId,
            del_flg:0
        });

        var pageSize = 1;
        entity.findFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            if (!items || items.length == 0) {
                return callback("数据不存在!", null)
            }

            var ret = {
                tgId: items[0].tg_id,
                tree: JSON.parse(items[0].data)
            };

            return callback(null, ret);
        });
    }

    //------------------------------------------------------------------
    /* 显示/隐藏题纲的部分节点
     * 参数 tree:  SJGTGV3
     * 参数 optionalIds: [45, 78, 99]
     * 参数 show: 1表示显示／0表示隐藏
     * */
    tg3Util.showTgTreeNode = function ( tree, optionalIds, show, reverse){
        for(var i=0; i<tree.root.children.length; i++){
            var one = tree.root.children[i];
            if(one){
                if(one.p==1) {
                    one.h = 1;// 隐藏保留项目
                }
                if(optionalIds.indexOf(parseInt(one.id))>=0){
                    one.h = show?0:1;
                }
            }
        }
    }

    //------------------------------------------------------------------
    /* 模糊匹配用: 显示题纲的所有可选节点
     * 参数 tree:  SJGTGV3
     * 参数 show: 1表示显示／0表示隐藏
     * */
    tg3Util.adjustTgTreeForFuzzyMatching = function (tree, show){
        for(var i=0; i<tree.root.children.length; i++){
            var one = tree.root.children[i];
            if(one){
                if(one.p==1){
                    one.h = 1;// 隐藏保留项目
                }
                if(one.o==1 && one.p!=1){
                    //one.h = show?0:1;
                    one.h = 1;
                }
            }
        }
    }

    /* 显示部分可选节点，隐藏剩余可选节点
     * 参数 tree:  SJGTGV3
     * 参数 optionalIds: [45, 78, 99]
     * */
    tg3Util.showAndHideTgTreeOptionNode = function ( tree, optionalIds){
        for(var i=0; i<tree.root.children.length; i++){
            var one = tree.root.children[i];
            if(one){
                if(one.p==1)    continue;

                if((one.o==1) // 非保留的可选节点
                || one.kind==SJGNode.SJGNODE_KIND_1){ // 数组节点当做可选节点
                    if(optionalIds.indexOf(parseInt(one.id))>=0){
                        one.h = 0;
                    }else{
                        one.h= 1;
                    }
                }
            }
        }
    }


    //------------------------------------------------------------------
    /* 根据特征值获取某个分页映射下的设计
     * 参数 tgId:  题纲id
     * 参数 eigen:  特征值,例如："[1,2,3]"
     * 参数 withFineTune： 1表示返回微调数据
     * 参数: callback：回调，参数如下：
     *          error: 错误消息
     *          data: 如果withFineTune为0，则返回下列数据：{
     *                      "productId":0,
     *                      "designId":321,
     *                      "coreStructure":{"version":3,"name":"会议邀请函","root":{}}
     *                }
     *                如果withFineTune为1，则返回下列数据：{
     *                      "bleedHeight": 36,
     *                      "bleedWidth": 36,
     *                      "coreStructure": {},            // 文档核心结构对象
     *                      "designId": 738,
     *                      "designNm": "红色线型简约名片",
     *                      "pageCount": 2,
     *                      "pageHeight": 709,
     *                      "pageWidth": 1134,
     *                      "productId": 0,
     *                      "productNm": "",
     *                      "textObjects": “",               //  微调初始化字符串
     *                      "typeL": 4
     *                 }
     * */
    tg3Util.getDesignByEigen = function (tgId,  eigen, withFineTune ,callback){

        var entity = new TgDesignDocModel({
        });
        entity.getDesignByEigen(tgId, eigen, 0/*计算模式*/, withFineTune ,function(error, items){
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            if (!items || items.length == 0) {
                return callback(null,null);
            }
            var designModel = items[0];
            if(withFineTune){
                tg3Util.getTgAndFineTuneInfoOfDesign(null, designModel.design_id, function(error, data){
                    if (error) {
                        console.log(error);
                        return callback(error, null);
                    }
                    if(data){
                        data.coreStructure = CommonUtil.process_before_JSON_Parse(data.coreStructure);
                        data.coreStructure = JSON.parse(data.coreStructure);
                    }
                    return callback(null, data);
                });

            }else{
                designModel.data = CommonUtil.process_before_JSON_Parse(designModel.data);
                var data = {
                    productId:0,
                    designId:designModel.design_id,
                    coreStructure:JSON.parse(designModel.data)
                };
                return callback(null ,data);
            }
        });
    }

    //------------------------------------------------------------------
    /* 获取与隐藏特征值最近的设计
     * 参数 tgId:  题纲id
     * 参数 eigen:  特征值,例如："[1,2,3]"
     * 参数 withFineTune： 1表示返回微调数据
     * 参数: callback：回调，参数如下：
     *          error: 错误消息
     *          data: 如果withFineTune为0，则返回下列数据：{
     *                      "productId":0,
     *                      "designId":321,
     *                      "coreStructure":{"version":3,"name":"会议邀请函","root":{}}
     *                }
     *                如果withFineTune为1，则返回下列数据：{
     *                      "bleedHeight": 36,
     *                      "bleedWidth": 36,
     *                      "coreStructure": {},            // 文档核心结构对象
     *                      "designId": 738,
     *                      "designNm": "红色线型简约名片",
     *                      "pageCount": 2,
     *                      "pageHeight": 709,
     *                      "pageWidth": 1134,
     *                      "productId": 0,
     *                      "productNm": "",
     *                      "textObjects": “",               //  微调初始化字符串
     *                      "typeL": 4
     *                 }
     * */
    tg3Util.getDesignByEigenOfHidden = function (tgId,  eigen, withFineTune ,callback){

        var entity = new TgDesignDocModel({
        });
        entity.getDesignByEigenOfHidden(tgId, eigen, function(error, items){
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            if (!items || items.length == 0) {
                return callback( null, null)
            }
            var designModel = items[0];
            if(withFineTune){
                tg3Util.getTgAndFineTuneInfoOfDesign(null, designModel.design_id, function(error, data){
                    if (error) {
                        console.log(error);
                        return callback(error, null);
                    }
                    if(data){
                        data.coreStructure = CommonUtil.process_before_JSON_Parse(data.coreStructure);
                        data.coreStructure = JSON.parse(data.coreStructure);
                    }
                    return callback(null, data);
                });

            }else{
                designModel.data = CommonUtil.process_before_JSON_Parse(designModel.data);
                var data = {
                    productId:0,
                    designId:designModel.design_id,
                    coreStructure:JSON.parse(designModel.data)
                };
                return callback(null ,data);
            }
        });
    };

    //------------------------------------------------------------------
    /*
     * 获取自动回复消息列表 TODO
     * 返回结果:
     *  [
     *      {
     *           "id": 1,
     *           "keyword": “订单,下单,发货",
     *           "typeDesc": “订单相关",
     *           "qaList": [
     *             {
     *               "id": 11,
     *               "q": "下单后多久能发货?",
     *               "a": "订单下单后一般需要一天左右的时间印刷",
     *               "qEx": {
     *                 "desc": "查询物流信息",
     *                 "url": "https://www.kuaidi100.com"
     *               }
     *             }
     *          ]
     *      }
     *  ]
     * */
    tg3Util.getAutoRelayInfo_v5_3 = function(version, callback) {
        var DEFAULT_QA = [
            {
                id: 1,
                keyword: '订单,下单,发货',
                typeDesc: '订单相关',
                qaList: [
                    {
                        id: 101,
                        q: '现在下单什么时候可以发货？（仅限名片）',
                        a: '当日17点之前的订单，当日开始印刷，次日下午17点可以发货。'+
                            '17点之后的订单，次日开始印刷，隔天中午可发货，物流下午会更新。'+
                            '印刷时间一般是1-2天。',
                        qEx: {
                            //
                        }
                    },
                    {
                        id: 102,
                        q: '发货后，多久可以收到货？',
                        a: '发货后，一般普快是3-5天，快递默认为百世汇通。特殊地区会发其他快递，以实际发送的快递为准。',
                        qEx: {
                            desc: '查询物流信息',
                            url: 'https://www.kuaidi100.com'
                        }
                    },
                    {
                        id: 103,
                        q: '我下了订单，但是发现错误需求修改？我该如何修改呢？',
                        a: '目前已支付状态的订单用户是不能自行修改的，需添加客服键妹微信：17762500830（yijianshengcheng）一键生成的小写全拼。',
                        qEx: {
                            desc: '武汉示界港科技有限公司',
                            url: 'http://www.itbour.com/'
                        }
                    },
                    {
                        id: 104,
                        q: '我的订单发货了没？快递到哪了？',
                        a: '联系键妹需添加客服键妹微信：17762500830（yijianshengcheng）一键生成的小写全拼。',
                        qEx: {
                            desc: '武汉示界港科技有限公司',
                            url: 'http://www.itbour.com/'
                        }
                    },
                    {
                        id: 105,
                        q: '我等不及了，快递可以换顺丰吗？',
                        a: '可以！我们给您退已支付的快递差价并给您改发顺丰到付。',
                        qEx: {
                            desc: '武汉示界港科技有限公司',
                            url: 'http://www.itbour.com/'
                        }
                    }
                ]
            }
        ];

        return callback(null, {qa:DEFAULT_QA, version:1});
    };

    //------------------------------------------------------------------
    // 默认微调用字体
    var DEFAULT_FINETUNE_FONTS = [
        {font:'安卓中文',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190531886_0bMMwQnA1ulPRVKnu7D0',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534896_ymL0a6FHjabk4WpHt1Kl'}, //--
        {font:'方正仿宋简体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190531552_8iJenUKrwkQddHeWMoZq',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534779_ZwV6HkToC6sewhXhFoA0'}, //--
        {font:'刻石錄明體',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534651_B8wExgNbE6PtNTPBKH29',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532514_HbOMrPb22RBL1gcW7fpb'}, //--
        {font:'方正黑体简体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534519_gJub8Zc78t5SQCRTQBjZ',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532797_S8ynhRwvAraRFtIT84Qm'}, //--
        {font:'方正楷体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534396_I1U5Lr7sWrD5qY2ARv9d',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532712_PR38HsnpeTFnpw4gf0eC'}, //--
        {font:'方正书宋简体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534298_jnjVcAdiZ7KjlCEB4VjC',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532605_HkOkQDjv1r1OCaFaMiw8'}, //--
        {font:'全字库正宋体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190534220_aKyN3hNmosGFTy0akOzj',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532468_ljbXtFmpMdFiIUi6ZIRx'}, // --
        {font:'台湾全字库正楷体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/1905349_xqAoqdMYDJbtpLy6zxwx',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532381_yfYhZ5EKuncCmvj8Btd4'}, // --
        {font:'文泉驿等宽正黑',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190533833_112c80Hxeh1MtUtSHoN8',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532306_ajNAU8iGC1BYi3DLaPXz'}, //--
        {font:'文泉驿微米黑',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190533647_5lSlCbUrchVR7x0WSeUa',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532236_i0y59oPRqLuOi6CFHZZv'}, // --
        {font:'文泉驿正黑',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190533424_FwEwwKQA8xUDSkEkFs8D',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532189_mcCqtjiHM2bLY91HjNoG'}, //--
        {font:'新蒂小丸子体小学生版',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190533229_ZOEqQC94HR5PFYX4auKq',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532141_x6jDsamGzdWEeFxEOBLN'}, //--
        {font:'站酷高端黑体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190533133_JOhBkvgE5lTfJ3OXCmne',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/19053297_WBcrNXYIeim9CN4XhSHf'}, //--
        {font:'站酷酷黑体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532994_KIKKJs7PAbBxLr3Qvg2X',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/19053254_mwisRGKTk0Jxy4Tc1n3E'}, //--
        {font:'站酷快乐体',thumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/190532857_J2OzN6lsSiDrTgbZj8Nz',hThumb:'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/08/21/1905329_H4Lz90pood6cMtCf7Aqx'} //--
    ];

    // 获取默认微调用字体
    tg3Util.getDefaultFinetuneFonts = function(callback){
        return callback(null, DEFAULT_FINETUNE_FONTS||[]);
    };

    //------------------------------------------------------------------
    /* 获取微调信息
     * 参数 docId:  用户文档id
     * 参数 userId:  用户id
     * 参数: callback
     * */
    tg3Util.getFineTuneOfUserDoc = function (docId, userId, callback){
        // check
        var ent = new TgUserDocModel({
            doc_id: docId,
            //user_id: userId,
            del_flg: 0
        });
        ent.getDocInfoById(function(err, ret){
            if(err){
                return callback(err, null);
            }

            var retData = {
                id: docId||0,
                bleed:36, // 出血3mm
                pageWidth: 0,
                pageHeight: 0,
                textObjects: '[]',
                fonts: DEFAULT_FINETUNE_FONTS||[],
            };

            if(ret && ret.length>0) {
                var docInfo = ret[0];

                retData.pageWidth = docInfo.width||0;
                retData.pageHeight = docInfo.height||0;
                retData.textObjects = docInfo.finetune_data||'[]';
                // 出血只有印品(印品分类id 为 4)才有
                retData.bleed = (docInfo.typeL == CONFIG.DOC_TYPE_4) ? 36 : 0;
                retData.typeL = docInfo.typeL;
                retData.typeM = docInfo.typeM;
                retData.typeId = docInfo.type_id;

                var pcEnt = new TgUserPageModel({
                    doc_id: docId,
                    del_flg:0
                });
                pcEnt.findCountFromDB(function(err2, count){
                    if(err2){
                        return callback(err2, null);
                    }
                    retData.pageCount = count;

                    // 判断finetune_data是否为空
                    if(retData.textObjects == '[]') {
                        tg3Util.initFineTuneOfUserDoc(docId, 1, function(err3, ftData){
                            retData.textObjects = ftData || '[]';
                            return callback(err3, retData);
                        });

                    } else {
                        return callback(null, retData);
                    }

                });

            } else {
                return callback(null, retData);
            }
        });
    }

    //------------------------------------------------------------------
    /*  获取(初始化)微调信息
     * 参数 docId:  用户文档id
     * 参数 updFlg: 是否更新DB
     * 参数: callback
     * */
    tg3Util.initFineTuneOfUserDoc = function (docId, updFlg, callback) {
        // 检索文档
        var mdm = new TgUserDocModel({
            doc_id:docId,
            del_flg:0
        });
        mdm.findFromDB(function(error, items){
            if(error) {
                return callback(error, null);
            }

            if(items && items.length>0) {
                var docInfo = items[0];

                if(docInfo.finetune_data && docInfo.finetune_data.length>0 && docInfo.finetune_data != '[]') {
                    // 存在则直接返回
                    return callback(null, docInfo.finetune_data);

                } else {
                    // 检索PageContent
                    var mpm = new TgUserPcModel({
                        doc_id:docId,
                        del_flg:0
                    });
                    mpm.findFromDBOrderByPageIndex(function(err2, retPcs){
                        if(err2) {
                            return callback(err2, null);
                        }

                        if(retPcs && retPcs.length>0) {
                            var pcs = retPcs;

                            var ftData = '[';
                            for(var i=0; i< pcs.length; i++) {
                                // 解析data
                                var strData = (pcs[i].data||'{}').replace(/\n/g,'\\n').replace(/\t/g,'').replace(/\r/g,'')
                                        .replace(/\\"/g,'\\\\\\"').replace(/[\x00-\x09\x0B-\x1F\x7F-\x9F]/g, "");
                                var sjgPg = new SJGPage(new GlobalData());
                                sjgPg.fromJsonOfTG(JSON.parse(strData), docInfo.ver);

                                // 开始拼接信息
                                if(i>0){
                                    ftData += ',';
                                }
                                ftData += '{';
                                // root
                                ftData += '"root":' + sjgPg.root.toJsonOfMinTextExt();
                                // replaces
                                if(docInfo.ver < SJGNode.VERSION_3){
                                    ftData += ',"replaces":'+(pcs[i].replaces||'[]');
                                }else{
                                    ftData += ',"replaces2":'+(pcs[i].replaces||'[]');
                                }
                                ftData += '}';
                            }
                            ftData += ']';

                            if(updFlg == 1) {
                                var updEnt = new TgUserDocModel({
                                    doc_id: docId,
                                    //finetune_flg: 0,
                                    finetune_data: ftData,
                                });
                                updEnt.updateToDB(function(err3){
                                    return callback(err3, ftData);
                                });

                            } else {
                                return callback(null, ftData);
                            }

                        } else {
                            return callback('No Doc Pcs', null)
                        }

                    });
                }

            } else {
                return callback('No Document', null)
            }
        },0,1);
    }

    //------------------------------------------------------------------
    /* 更新微调字段
     * 参数 docId:  用户文档id
     * 参数 userId:  用户id
     * 参数 ftFlg:  微调状态
     * 参数 ftData:  微调数据
     * 参数: callback
     * */
    tg3Util.updFineTuneOfUserDoc = function (docId, userId, ftFlg, ftData, callback){
        // check
        var ent = new TgUserDocModel({
            doc_id: docId,
            user_id: userId,
            del_flg: 0
        });
        ent.findFromDB(function(err, ret){
            if(err){
                return callback(err, null);
            }

            if(ret && ret.length>0) {
                // update finetune
                var updEnt = new TgUserDocModel({
                    doc_id: docId,
                    finetune_flg: ftFlg,
                    finetune_data: ftData
                });
                updEnt.updateToDB(function(updErr){
                    if (updErr) {
                        return callback(updErr,null);
                    }
                    // delete page/pc
                    // 逻辑删除page
                    var delPgEnt = new TgUserPageModel({
                        doc_id: docId,
                        update_by: userId,
                        thumb:null
                    });
                    delPgEnt.clrPageThumb(function(delErr) {
                        if (delErr) {
                            return callback(delErr,null);
                        }

                        // 逻辑删除pc
                        var delPcEnt = new TgUserPcModel({
                            doc_id: docId,
                            update_by: userId,
                            thumb_s:null
                        });
                        delPcEnt.clrPcThumb(function (delErr2) {
                            if (delErr2) {
                                return callback(delErr2,null);
                            }
                            return callback(null, null);
                        });


                    });
                });

            } else {
                return callback('未找到用户文档', null);
            }
        });
    }


    //------------------------------------------------------------------
    /* 分词
     * 参数 : txt 字符串
     * 返回 : 分词后的字符串数组
     * */
    tg3Util.doSegmentOfText = function(txt,min,max){
        if(!txt)    return null;
        // 只能2-6个字以内分词
        min = min || 2;
        max = max || 6;

        if(txt.length>max || txt.length<min){
            return [txt]
        };

        var segs = segment.doSegment(txt, {stripPunctuation: true});
        var segWords=[];
        for(var i=segs.length-1 ; i>=0;i--){
            var one = segs[i];
            if( one.p == POSTAG.D_N
                ||one.p == POSTAG.D_S
                ||one.p == POSTAG.A_NR
                ||one.p == POSTAG.A_NS
                ||one.p == POSTAG.D_A){

                // 过滤掉部分词语，例如：色
                var w = one.w.replace(/色/g,'');

                segWords.push(w);
            }
        }
        // 分词失败, 结果为空
        if(segWords.length == 0) {
            segWords.push(txt);
        }


        return segWords;
    }

    //---------------------------------------------------------------------------------
    /**
     * 确认用户是否已购买模板
     * 返回值: {“success":1,"msg":"成功","data":{"designId":888,"price":1000,"paid":1,"desc":"原价10元, 折扣价9.8, 是否确认购买"}}
     */
    tg3Util.chkBuyTmplInfo = function(userId,type,tmplId,callback){
        // 检索模版价格信息
        (new ItbTmplPriceModel).findPriceInfoByType(type,tmplId,0,1,function(err,ret){
            if (err) {
                console.log(err);
                return callback(err, null);
            }

            if(ret && ret.length>0) {
                var priceInfo = ret[0];
                var priceYuan = Number(priceInfo.price||'0')/100;

                var desc='';
                if(Number(priceInfo.free||'0') == 1) {
                    desc = '原价'+priceYuan+'元，限时免费中，是否确认购买？';
                } else {
                    desc = '售价'+priceYuan+'元，是否确认购买？';
                }

                // 检索购买纪录
                (new ItbBuyTmplModel).chkBuyTmplInfo(userId,type,tmplId,function(error,items) {// 结果按valid排序
                    if (error) {
                        console.log(error);
                        return callback(error, null);
                    }

                    if(items && items.length>0) {
                        // 已购买
                        var buyInfo = items[0];
                        if(buyInfo.valid==1) {
                            // 有效期内
                            return callback(null,{
                                designId:tmplId,
                                price:Number(priceInfo.price||'0'),
                                paid:1,
                                desc:'该模版已购买，可直接使用。'
                            });
                        } else {
                            // 过期
                            var retData = {
                                designId:tmplId,
                                price:Number(priceInfo.price||'0'),
                                paid:0,
                                desc:desc
                            };

                            if(buyInfo.state==60) {// 状态为[过期]
                                return callback(null,retData);
                            } else {
                                // 其他状态需更新为[过期] TODO
                                payUtil.updOrderState(buyInfo.buy_id, 30, 60, buyInfo.total_fee, buyInfo.user_id,
                                    function(updErr, updRet){
                                        return  callback(updErr, retData);
                                    }
                                );
                            }
                        }

                    } else {
                        // 未购买
                        return callback(null,{
                            designId:tmplId,
                            price:Number(priceInfo.price||'0'),
                            paid:0,
                            desc:desc
                        });
                    }
                });

            } else {
                // 免费模版or模版不存在
                return callback(null,{
                    designId:tmplId,
                    price:0,
                    paid:0,
                    desc:'非付费模版',// TODO
                });
            }
        });
    }

    //------------------------------------------------------------------
    /**
     *  获取用户已购作品列表
     */
    tg3Util.getBuyTmplListByUser = function(userId,pageNum,pageSize,callback){
        var skipNum = Number(pageNum||'0')*Number(pageSize||'12');
        (new ItbBuyTmplModel).getBuyTmplListByUser(userId,skipNum,Number(pageSize||'12'),function(error,items) {
            if (error) {
                console.log(error);
                return callback(error, null);
            }
            var retInfo = [];
            for(var i=0;i<items.length;i++){
                var msg = '购买于 '+items[i].create_date;
                if(Number(items[i].expired||'0')==1){
                    msg = '已过使用期';
                }
                retInfo.push({
                    typeL: items[i].type,
                    designId: items[i].tmpl_id,
                    name: items[i].tmpl_name||'',
                    price: items[i].total_fee,// TODO
                    buyTime: items[i].create_date||'',
                    deadline: items[i].valid_date_end||'',
                    thumb: items[i].tmpl_thumb||'',
                    expired: items[i].expired,
                    msg:msg,
                });
            }
            return callback(null, retInfo);
        });
    };

    //------------------------------------------------------------------


    /**
     *  获取海报列表
     *  参数：typeM 二级分类ID
     *  参数：colors	所需颜色分类 id 数组的 json 字符串, 鉴于网页端可随时发布, 颜色对应可直接写死, 如需多选, 将所选验证传递即可, 如: "[1, 2, 3]"
     *  参数: tags	所需热门标签 id 数组的 json 字符串, 鉴于网页端可随时发布, 标签对应可直接写死, 如需多选, 将所选标签传递即可, 如: "[1, 2]"
     *  参数: withTotalPage 是否需要总页数
     *  参数: pageNum 请求页码, 从 0 开始
     *  参数: pageSize 请求页大小
     *  参数：callback，参数如下：
     *          error：null表示无错误
     *          data：对象结构，例如：
     *              {
     *                 "list":[
     *                      {
     *                          "id":515,
     *                          "thumb":"http://itbour-generate.oss-cn-hangzhou.aliyuncs.com/image/U3/2017/02/07/153525097_fBkxBZpSet3FHB9K9CWS/0.jpg",
     *                          "typeL":1,
     *                          "title":"大声说爱你情人节祝福模板",
     *                          "desc":"2.00元"
     *                      }
     *                  ],
     *                  "totalPage":10,
     *                  "currentPage":0
     *              }
     */
    tg3Util.getFilterListForWebOfPoster  = function(typeM,colors,tags, withTotalPage, pageNum, pageSize,callback){
        // 查询列表
        var entity =(new TgDesignDocModel);
        entity.getFilterListForWebOfPoster(typeM,colors,tags, withTotalPage, pageNum, pageSize,function(err,ret){
            if(ret ){
                // 查询个数
                entity.getFilterListCountForWebOfPoster(typeM,colors,tags, withTotalPage, pageNum, pageSize,function(err,count){
                    if(count ){
                        var totalPage = Math.floor((count-1)/pageSize)+1;
                        var data = {
                            list:ret,
                            totalPage:totalPage,
                            currentPage:pageNum
                        };
                        return callback(null, data);

                    }else{
                        return callback(err,{list:[]});
                    }

                });

            }else{
                return callback(err,{list:[]});
            }

        });
    }
    //------------------------------------------------------------------


    /**
     *  获取印品列表
     *  参数：typeM 二级分类ID
     *  参数：colors	所需颜色分类 id 数组的 json 字符串, 鉴于网页端可随时发布, 颜色对应可直接写死, 如需多选, 将所选验证传递即可, 如: "[1, 2, 3]"
     *  参数: tags	所需热门标签 id 数组的 json 字符串, 鉴于网页端可随时发布, 标签对应可直接写死, 如需多选, 将所选标签传递即可, 如: "[1, 2]"
     *  参数: withTotalPage 是否需要总页数
     *  参数: pageNum 请求页码, 从 0 开始
     *  参数: pageSize 请求页大小
     *  参数：callback，参数如下：
     *          error：null表示无错误
     *          data：对象结构，例如：
     *              {
     *                 "list":[
     *                      {
     *                          "id":515,
     *                          "thumb":"http://itbour-generate.oss-cn-hangzhou.aliyuncs.com/image/U3/2017/02/07/153525097_fBkxBZpSet3FHB9K9CWS/0.jpg",
     *                          "typeL":1,
     *                          "title":"大声说爱你情人节祝福模板",
     *                          "desc":"印刷 20元／2盒"
     *                      }
     *                  ],
     *                  "totalPage":10,
     *                  "currentPage":0
     *              }
     */
    tg3Util.getFilterListForWebOfPrint  = function(typeM,colors,tags, withTotalPage, pageNum, pageSize,callback){
        // 查询列表
        var entity =(new ItbItemsModel);
        entity.getFilterListForWebOfPrint(typeM,colors,tags, withTotalPage, pageNum, pageSize,function(err,ret){
            if(ret ){
                // 查询个数
                entity.getFilterListCountForWebOfPrint(typeM,colors,tags, withTotalPage, pageNum, pageSize,function(err,count){
                    if(count ){
                        var totalPage = Math.floor((count-1)/pageSize)+1;
                        var data = {
                            list:ret,
                            totalPage:totalPage,
                            currentPage:pageNum
                        }
                        return callback(null, data);

                    }else{
                        return callback(err,{list:[]});
                    }

                });

            }else{
                return callback(err,{list:[]});
            }

        });
    }
    //------------------------------------------------------------------

    // todo 新增接口待审核 begin
    tg3Util.get_fastCreateTypeLAndTypeMs_v5_2 = function (callback) {
        //var items = [{"id":1,"name":"设计","sub":[{"id":10,"name":"邀请函","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_invitation.png","sub":[{"id":101,"name":"开业","icon":""},{"id":103,"name":"展销会","icon":""},{"id":104,"name":"发布会","icon":""},{"id":105,"name":"节日活动","icon":""},{"id":106,"name":"会议","icon":""},{"id":107,"name":"招聘会","icon":""},{"id":108,"name":"直播","icon":""},{"id":802,"name":"婚礼","icon":""}]},{"id":20,"name":"促销","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_promotion.png","sub":[{"id":201,"name":"节日","icon":""},{"id":202,"name":"季节","icon":""},{"id":203,"name":"特卖","icon":""},{"id":204,"name":"开业","icon":""},{"id":205,"name":"新品","icon":""}]},{"id":30,"name":"求职招聘","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_job.png","sub":[{"id":301,"name":"招聘启事","icon":""}]},{"id":50,"name":"推广","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_spread.png","sub":[{"id":505,"name":"企业介绍","icon":""}]},{"id":90,"name":"通知","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_notify.png","sub":[]},{"id":80,"name":"其它","icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_other.png","sub":[{"id":801,"name":"节日祝福","icon":""},{"id":501,"name":"活动通知","icon":""},{"id":502,"name":"培训通知","icon":""},{"id":503,"name":"放假通知","icon":""},{"id":504,"name":"会议通知","icon":""}]}]},{"id":4,"name":"印品","sub":[{"id":40100,"name":"名片","icon":"","sub":[]},{"id":40200,"name":"传单","icon":"","sub":[]},{"id":40300,"name":"展架","icon":"","sub":[]}]}];
        var items = [
            {
                "id":1,
                "name":"设计",
                "sub":[
                    {
                        "id":10,
                        "name":"邀请函",
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821710_xD9WdeVxXgFtS7rPVIcN",//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_invitation.png"
                        "sub":[
                            {
                                "id":101,
                                "name":"开业",
                                "icon":""
                            },
                            {
                                "id":103,
                                "name":"展销会",
                                "icon":""
                            },
                            {
                                "id":104,
                                "name":"发布会",
                                "icon":""
                            },
                            {
                                "id":105,
                                "name":"节日活动",
                                "icon":""
                            },
                            {
                                "id":106,
                                "name":"会议",
                                "icon":""
                            },
                            {
                                "id":107,
                                "name":"招聘会",
                                "icon":""
                            },
                            {
                                "id":108,
                                "name":"直播",
                                "icon":""
                            },
                            {
                                "id":802,
                                "name":"婚礼",
                                "icon":""
                            }
                        ]
                    },
                    {
                        "id":20,
                        "name":"促销",
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821154_OlAFri8KB21MXkSlZpRo",//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_promotion.png"
                        "sub":[
                            {
                                "id":201,
                                "name":"节日",
                                "icon":""
                            },
                            {
                                "id":202,
                                "name":"季节",
                                "icon":""
                            },
                            {
                                "id":203,
                                "name":"特卖",
                                "icon":""
                            },
                            {
                                "id":204,
                                "name":"开业",
                                "icon":""
                            },
                            {
                                "id":205,
                                "name":"新品",
                                "icon":""
                            }
                        ]
                    },
                    {
                        "id":30,
                        "name":"求职招聘",
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821455_txxl43VAeSMA7aH6nHkz",//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_job.png"
                        "sub":[
                            {
                                "id":301,
                                "name":"招聘启事",
                                "icon":""
                            }
                        ]
                    },
                    {
                        "id":50,
                        "name":"推广",
                        "icon":'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821498_IMZJZZBJasYf9UpaaVu1',//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_spread.png",
                        "sub":[
                            {
                                "id":505,
                                "name":"企业介绍",
                                "icon":""
                            }
                        ]
                    },
                    {
                        "id":90,
                        "name":"通知",
                        "icon":'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821606_3xlM0vDR3aIj0ie0UhvD',//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_notify.png",
                        "sub":[

                        ]
                    },
                    {
                        "id":80,
                        "name":"其它",
                        "icon":'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821307_3csUt7vPzS64qCPS3MTy',//"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_other.png",
                        "sub":[
                            {
                                "id":801,
                                "name":"节日祝福",
                                "icon":""
                            },
                            {
                                "id":501,
                                "name":"活动通知",
                                "icon":""
                            },
                            {
                                "id":502,
                                "name":"培训通知",
                                "icon":""
                            },
                            {
                                "id":503,
                                "name":"放假通知",
                                "icon":""
                            },
                            {
                                "id":504,
                                "name":"会议通知",
                                "icon":""
                            }
                        ]
                    }
                ]
            },
            {
                "id":4,
                "name":"印品",
                "sub":[
                    {
                        "id":40100,
                        "name":"名片",
                        "icon":"",
                        "sub":[

                        ]
                    },
                    {
                        "id":40200,
                        "name":"传单",
                        "icon":"",
                        "sub":[

                        ]
                    },
                    {
                        "id":40300,
                        "name":"展架",
                        "icon":"",
                        "sub":[

                        ]
                    }
                ]
            }
        ];
        return callback(null, items);
    };
    // todo 新增接口待审核 end

    // todo 新增接口待审核 begin
    tg3Util.get_fastCreateTypeLAndTypeMs_v5_3 = function (callback) {
        //var items = [{"id":6,"name":"社交媒体","sub":[{"icon":"","id":60100,"name":"公众号封面大图","sub":[]},{"icon":"","id":60200,"name":"公众号封面小图","sub":[]},{"icon":"","id":60300,"name":"微博封面图","sub":[]}]},{"id":1,"name":"海报","sub":[{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_invitation.png","id":10,"name":"邀请函","sub":[{"icon":"","id":101,"name":"开业"},{"icon":"","id":103,"name":"展销会"},{"icon":"","id":104,"name":"发布会"},{"icon":"","id":105,"name":"节日活动"},{"icon":"","id":106,"name":"会议"},{"icon":"","id":107,"name":"招聘会"},{"icon":"","id":108,"name":"直播"},{"icon":"","id":802,"name":"婚礼"}]},{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_promotion.png","id":20,"name":"促销","sub":[{"icon":"","id":201,"name":"节日"},{"icon":"","id":202,"name":"季节"},{"icon":"","id":203,"name":"特卖"},{"icon":"","id":204,"name":"开业"},{"icon":"","id":205,"name":"新品"}]},{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_job.png","id":30,"name":"求职招聘","sub":[{"icon":"","id":301,"name":"招聘启事"}]},{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_spread.png","id":50,"name":"推广","sub":[{"icon":"","id":505,"name":"企业介绍"}]},{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_notify.png","id":90,"name":"通知","sub":[]},{"icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_other.png","id":80,"name":"其它","sub":[{"icon":"","id":801,"name":"节日祝福"},{"icon":"","id":501,"name":"活动通知"},{"icon":"","id":502,"name":"培训通知"},{"icon":"","id":503,"name":"放假通知"},{"icon":"","id":504,"name":"会议通知"}]}]},{"id":4,"name":"印品","sub":[{"icon":"","id":40100,"name":"名片","sub":[]},{"icon":"","id":40200,"name":"传单","sub":[]},{"icon":"","id":40300,"name":"展架","sub":[]}]}];
        var items = [
            {
                "id":6,
                "name":"社交媒体",
                "sub":[
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145802526_ZVTfurVhVEvHLOe5h2O8",
                        "id":60100,
                        "name":"公众号首图",
                        "sub":[

                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145802726_M8M1YatVN9kGwUOMxFfd",
                        "id":60200,
                        "name":" 公众号小图",
                        "sub":[

                        ]
                    }
                ]
            },
            {
                "id":1,
                "name":"海报",
                "sub":[
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821710_xD9WdeVxXgFtS7rPVIcN",
                        "id":10,
                        "name":"邀请函",
                        "sub":[
                            {
                                "icon":"",
                                "id":101,
                                "name":"开业"
                            },
                            {
                                "icon":"",
                                "id":103,
                                "name":"展销会"
                            },
                            {
                                "icon":"",
                                "id":104,
                                "name":"发布会"
                            },
                            {
                                "icon":"",
                                "id":105,
                                "name":"节日活动"
                            },
                            {
                                "icon":"",
                                "id":106,
                                "name":"会议"
                            },
                            {
                                "icon":"",
                                "id":107,
                                "name":"招聘会"
                            },
                            {
                                "icon":"",
                                "id":108,
                                "name":"直播"
                            },
                            {
                                "icon":"",
                                "id":802,
                                "name":"婚礼"
                            }
                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821154_OlAFri8KB21MXkSlZpRo",
                        "id":20,
                        "name":"促销",
                        "sub":[
                            {
                                "icon":"",
                                "id":201,
                                "name":"节日"
                            },
                            {
                                "icon":"",
                                "id":202,
                                "name":"季节"
                            },
                            {
                                "icon":"",
                                "id":203,
                                "name":"特卖"
                            },
                            {
                                "icon":"",
                                "id":204,
                                "name":"开业"
                            },
                            {
                                "icon":"",
                                "id":205,
                                "name":"新品"
                            }
                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821455_txxl43VAeSMA7aH6nHkz",
                        "id":30,
                        "name":"求职招聘",
                        "sub":[
                            {
                                "icon":"",
                                "id":301,
                                "name":"招聘启事"
                            }
                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821498_IMZJZZBJasYf9UpaaVu1",
                        "id":50,
                        "name":"推广",
                        "sub":[
                            {
                                "icon":"",
                                "id":505,
                                "name":"企业介绍"
                            }
                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821606_3xlM0vDR3aIj0ie0UhvD",
                        "id":90,
                        "name":"通知",
                        "sub":[

                        ]
                    },
                    {
                        "icon":"http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821307_3csUt7vPzS64qCPS3MTy",
                        "id":80,
                        "name":"其它",
                        "sub":[
                            {
                                "icon":"",
                                "id":801,
                                "name":"节日祝福"
                            },
                            {
                                "icon":"",
                                "id":501,
                                "name":"活动通知"
                            },
                            {
                                "icon":"",
                                "id":502,
                                "name":"培训通知"
                            },
                            {
                                "icon":"",
                                "id":503,
                                "name":"放假通知"
                            },
                            {
                                "icon":"",
                                "id":504,
                                "name":"会议通知"
                            }
                        ]
                    }
                ]
            },
            {
                "id":4,
                "name":"印品",
                "sub":[
                    {
                        "icon":"",
                        "id":40100,
                        "name":"名片",
                        "sub":[

                        ]
                    },
                    {
                        "icon":"",
                        "id":40200,
                        "name":"传单",
                        "sub":[

                        ]
                    },
                    {
                        "icon":"",
                        "id":40300,
                        "name":"展架",
                        "sub":[

                        ]
                    }
                ]
            }
        ];
        return callback(null, items);
    };
    // todo 新增接口待审核 end

    //-------------------------------------v5.2 搜索工具方法 begin-----------------------------------
    // todo 新增接口 待审核 begin
    tg3Util.get_search_v5_2 = function (keyword, typeLs, pageNum, pageSize, callback){
        // 分词处理
        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 拼接typeL
        var arrTypeL = JSON.parse(typeLs||'[]');
        var pTypeLs = '';
        if(arrTypeL && arrTypeL.length>0) {
            pTypeLs = arrTypeL.join(',');
        }

        // 搜索
        var entity = new TgKeywordModel();
        entity.search(segKeyword, pTypeLs, pageNum, pageSize, callback);
    }
    tg3Util.get_search_v5_2_Cnt = function (keyword, typeLs, callback) {
        var entity = new TgKeywordModel();

        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 拼接typeL
        var arrTypeL = JSON.parse(typeLs||'[]');
        var pTypeLs = '';
        if(arrTypeL && arrTypeL.length>0) {
            pTypeLs = arrTypeL.join(',');
        }
        entity.searchCnt(segKeyword, pTypeLs, callback);
    }
    // todo 新增接口 待审核 end
    //-------------------------------------v5.2 搜索工具方法 end-----------------------------------



    //------------------------------------------- APP 端 v5_3 相关接口 --------------------------------
    /**获取模板/作品提纲微调信息
     * 参数:device	0表示从p0 表示从pc来的请求
     *              1 标识一键生成iOS端
     *              2 标识一键生成android端
     *              3  标识一键生成 app 嵌入网页
     *              4 标识一键生成 web 端
     *              5 标识一键生成微信端
     *              101 表示pc美化大师/102表示pc wps/103表示wps android/104表示wps ios
     *              201 标识利楚商务
     *              301 标识酒单网
     * 参数: isDesign	是否为设计标记
     * 参数: typeL	0 未分类   1 海报  2 多页  3 ppt. 4, 印品, 6, 社交媒体
     * 参数: docId	 对应文档 id, 可能是模板 id 也可能是用户文档 id, 根据 tmplFlg 区分
     * 参数: userId	用户唯一标识
     * 返回结果: callback,参数如下
     *          error：null 表示无错误
     *          data：结构如下：
     *               {
     *                 "bleedHeight": 36,
     *                 "bleedWidth": 36,
     *                 "coreStructure": {},            // 文档核心结构字符串
     *                 "designId": 738,
     *                 "designNm": "红色线型简约名片",
     *                 "pageCount": 2,
     *                 "pageHeight": 709,
     *                 "pageWidth": 1134,
     *                 "productId": 0,
     *                 "productNm": "",
     *                 "textObjects": “",               //  微调初始化字符串
     *                 "typeL": 4
     *               }
     *
     * */
    // todo 模拟接口, 待实现, begin
    tg3Util.getTgAndFineTuneInfo_v5_3 = function  (isDesign, typeL,docId, userId,  callback) {
        // 模版
        if(isDesign){
            tg3Util.getTgAndFineTuneInfoOfDesign(typeL, docId,function(error, data){

                if(error) {
                    return callback(error, null);
                }

                // 验证 vip
                if(data && data.vip == 10) {
                    (new ItbMemberModel()).checkUserVipType(userId, function(checkErr, vip){

                        if(checkErr) {
                            return callback(checkErr, null);
                        }

                        if(vip == data.vip) {
                            return callback(error, data);
                        } else {
                            return callback('此模板为 vip 免费模板, 您非 vip 会员, 请开通后再试', null);
                        }
                    });
                }
                // 非 vip 模板直接通过
                else {
                    return callback(error, data);
                }
            });
        }else{
            // 作品
            return tg3Util.getTgAndFineTuneInfoOfUserDoc(typeL, docId,callback);
        }
    }
    tg3Util.getTgAndFineTuneInfo_v5_3_forDebug = function  (isDesign, typeL,docId, userId,  callback) {
        // 模版
        if(isDesign){
            tg3Util.getTgAndFineTuneInfoOfDesign(typeL, docId,callback);
        }else{
            // 作品
            return tg3Util.getTgAndFineTuneInfoOfUserDoc(typeL, docId,callback);
        }
    }

    /**修正模板的提纲树
     * 参数: pcData	数组类型，数组元素为结构体，包括ver和data
     * 参数: ver 版本号
     * 返回结果: 修正后的模版的提纲树的Json
     * */
    tg3Util.SJGPagesFromPCData = function(pcData, ver){

        var ret = [];
        for(var i=0;i<pcData.length;i++){
            var one = pcData[i];
            if(one){
                var strFtData = CommonUtil.process_before_JSON_Parse(one.data);
                var sjgPg =new SJGPage(new GlobalData());
                sjgPg.fromJsonOfTG(JSON.parse(strFtData), ver);

                ret.push(sjgPg);
            }
        }
        return ret;
    }

    /**修正模板的提纲树
     * 参数: data	模版的提纲树的Json
     * 参数: pages	数组类型，数组元素为SJGPage
     * 参数: ver 版本号
     * 返回结果: 修正后的模版的提纲树的Json
     * */
    tg3Util.fixContentOfTgTree = function(data, pages, ver){
        //return debugUtil.fixContentOfTgTree(data, pages, ver);

        data = CommonUtil.process_before_JSON_Parse(data);

        var tree = null;
        if(ver<SJGNode.VERSION_3){
            tree =new SJGTG();
            tree.fromJson(JSON.parse(data),ver);
        }else{
            tree =new SJGTGV3();
            tree.fromJson(JSON.parse(data),ver);
        }
        for(var i=0;i<pages.length;i++){
            var sjgPg = pages[i];
            if(sjgPg){
                var rs = null;
                if(ver<SJGNode.VERSION_3){
                    rs = sjgPg.replaces;
                }else{
                    rs = sjgPg.replaces2;
                }
                // 更新文档提纲树中的value
                for(var n=0;n<rs.length;n++) {
                    var one = rs[n];
                    if(one ){
                        var f = tree.root.getNodeById(one.id);
                        if(f){
                            var o = null;
                            if(ver<SJGNode.VERSION_3){
                                o = sjgPg.root.getChildById(one.obj_id);
                            }else{
                                for(var t=0;t<one.relations.length;t++){
                                    var rel = one.relations[t];
                                    if(rel && rel.page_index==i){
                                        o = sjgPg.root.getChildById(rel.obj_id);
                                        break;
                                    }
                                }
                            }
                            if(!o) continue;
                            // 分类型处理
                            if(f.type == SJGNode.SJGNODE_TYPE_1){
                                // 文字
                                f.value.value = o.te;
                                f.value.max = o.te.length;
                                f.value.tip = o.te;// 提示

                            } else if(f.type == SJGNode.SJGNODE_TYPE_2){
                                // 图片
                                f.value.value = o.source;
                                f.value.pos = o.image; // 注意这里：提纲图片节点的pos指的是图片的真实宽高，与文档树中的图片对象image含义相同
                                f.value.mask = o.mask;
                            } else if(f.type == SJGNode.SJGNODE_TYPE_4) {
                                // 表格
                                if(o.children && o.children.length>0) {
                                    f.value.children = [];
                                    for(var rIdx=0;rIdx< o.children.length;rIdx++) {
                                        var row = o.children[rIdx];

                                        var repRow = new SJGReplaceItemTableRow();
                                        repRow.id = row.id;
                                        repRow.rowType = row.rowType;

                                        for(var cIdx=0;cIdx<row.children.length;cIdx++) {
                                            var cell = row.children[cIdx];

                                            var txt = cell.children[0];// TextExtObj
                                            var repTxt = new SJGReplaceItemText();
                                            repTxt.id = txt.id;
                                            repTxt.value = txt.te||'';
                                            repTxt.max = repTxt.value.length;
                                            repTxt.tip = repTxt.value;// 提示

                                            var repCell = new SJGReplaceItemTableCell();
                                            repCell.id = cell.id;
                                            repCell.children.push(repTxt);

                                            repRow.children.push(repCell);
                                        }

                                        f.value.id = o.id;
                                        f.value.children.push(repRow);
                                    }
                                    // 根据模版表格修正最大行数和列数
                                    f.value.maxRow = o.children.length> f.value.maxRow? o.children.length: f.value.maxRow ;
                                    f.value.maxCol = o.children[0].children.length+3; // 模版列数加3
                                }
                            } else if(f.type == SJGNode.SJGNODE_TYPE_5){
                                // 图形
                                f.value.gt = o.gt;
                                f.value.cp = o.cp.copy();
                            }
                        }
                    }
                }
            }
        }
        return tree.toJson();
    };

    /**创建模板的微调数据
     * 参数: pages	数组类型，数组元素为SJGPage
     * 参数: ver 版本号
     * 返回结果: fineTuneInfo：微调数据的字符串,错误返回null
     * */
    tg3Util.createFineTuneInfoOfDesignCore = function  (pages, ver) {
        // 从每个pc中按照SJGPage解析，提取其root\replaces\replaces2,并转换成json数组
        var ftData = '[';
        for(var i=0;i<pages.length;i++){
            var one = pages[i];
            if(one){
                var sjgPg =one;
                if(i>0){
                    ftData += ',';
                }
                ftData += '{';
                // root
                ftData += '"root":' + sjgPg.root.toJsonOfMinTextExt();
                // replaces
                if(ver<SJGNode.VERSION_3){
                    ftData += ',"replaces":';
                    ftData += '[';
                    for(var n=0;n<sjgPg.replaces.length;n++) {
                        if(n>0) {
                            ftData += ',';
                        }
                        ftData += sjgPg.replaces[n].toJson();
                    }
                    ftData += ']';
                }else{
                    ftData += ',"replaces2":';
                    ftData += '[';
                    for(var n=0;n<sjgPg.replaces2.length;n++) {
                        if(n>0) {
                            ftData += ',';
                        }
                        ftData += sjgPg.replaces2[n].toJson();
                    }
                    ftData += ']';
                }
                // arrLink
                if(sjgPg.arrLink && sjgPg.arrLink.length>0){
                    ftData+=',';
                    ftData+='"arrLink":';
                    ftData+='[';
                    for(var j=0;j<sjgPg.arrLink.length;j++) {
                        if(j>0) {
                            ftData+=',';
                        }
                        ftData+=sjgPg.arrLink[j].toJson();
                    }
                    ftData+=']';
                }
                ftData += '}';
            }
        }
        ftData += ']';
        return ftData.length==2?null:ftData;
    }
    /**向image服务发起请求，创建模板的微调数据，并更新到数据库
     * 参数: designId	模板 id
     * 参数: designData   design的data
     * 返回结果: callback,参数如下
     *          error：null 表示无错误
     *          fineTuneInfo：微调数据的字符串
     *          coreStructure:微调后的题纲数据的字符串
     * 注意：本质时候将createFineTuneInfoOfDesign的内容移动到image服务中(使用svrender3进程)，以便减少对js代码的依赖
     * */
    tg3Util.createFineTuneInfoOfDesignByHTTP = function  (designId, callback) {
        var url = CONFIG.ADJUST_DESIGN_FINETUNE_AND_TG+'?'+'designId='+designId;

        superagent.get(url)
            .end(function(err, res){
                if(err){
                    console.log('tg3Util.createFineTuneInfoOfDesignByHTTP Err: ' + err);
                    return callback(err, null,null);
                }

                var retJson = JSON.parse(res.text);
                if(retJson.success!=1){
                    return callback(' tg3Util.createFineTuneInfoOfDesignByHTTP Err ', null,null);

                }

                // 从db读取数据
                var mdm = new TgDesignDocModel({
                    design_id:designId,
                    del_flg:0
                });

                mdm.findFromDB(function(error, items){
                    if(error || !items || items.length <= 0) {
                        return callback(error || '获取 design 数据失败',null,null)
                    }
                    var docEntity = items[0];
                    return callback(null, docEntity.finetune_data, docEntity.data);

                },0,1);


            }
        );
    }

    /** @deprecated
     * 创建模板的微调数据，并更新到数据库
     * 参数: designId	模板 id
     * 参数: designData   design的data
     * 返回结果: callback,参数如下
     *          error：null 表示无错误
     *          fineTuneInfo：微调数据的字符串
     *          coreStructure:微调后的题纲数据的字符串
     * */
    tg3Util.createFineTuneInfoOfDesign = function  (designId, callback) {
        var entity0 = new TgDesignDocModel({
            design_id:designId,
            del_flg:0
        });
        entity0.findFromDB(function(error,items) {
            if (error || !items || items.length == 0) {
                console.log(error);
                return callback(error, null);
            }
            var designData = items[0];
            var entity = new TgDesignPcModel({
                design_id:designId,
                del_flg:0
            });

            entity.getPcDataOfDesign(function(error, pcData){
                if(!pcData || pcData.length==0){
                    return callback(error, null);
                }else{
                    // 生成微调数据
                    // 1、根据pc创建Page
                    // tg_design_pc的data中存放的是完整的SJGPage数据，例如：
                    //  {
                    //    "replaces2": [...], // 当前页用到的提纲页节点的数组
                    //    "describe": {...},
                    //    "root": {...},
                    //    "ct": 405
                    //  }
                    //  而td_design_doc的微调数据finetune_data
                    //  [{
                    //     "replaces2": [...],
                    //     "root": {...}
                    //  }]
                    //
                    // tg_design_pc的replaces中存放的是是SJGPage的replaces2数据【貌似暂时无用】
                    // tg_design_doc的data中存放的是完整的提纲数据，例如：
                    //  {
                    //    "version": 3,
                    //    "root": {...},
                    //    "name": "名片无映射版",
                    //    "ct": 903
                    //  }

                    var pages = tg3Util.SJGPagesFromPCData(pcData, designData.ver);
                    // 2、将pc的data转换成微调数据
                    var fineTuneInfo = tg3Util.createFineTuneInfoOfDesignCore(pages,designData.ver);
                    // 3、设计师修改了提纲对应的文字/图片等内容后保存到db时，tg_design_pc的data（SJGData的root中的Obj对象和replaces2中提纲节点对象）被更新，
                    // 但是tg_design_doc的data（提纲）未更新，所以需要利用SJGPage数据来修正提纲数据
                    var data = tg3Util.fixContentOfTgTree(designData.data,pages, designData.ver);
                    if(!fineTuneInfo || !data){
                        return callback('生成模版的微调数据,修正文档树,失败！',null);
                    }else{
                        // 更新到模版中
                        var updEnt = new TgDesignDocModel({
                            design_id: designId,
                            data:data,
                            //finetune_flg: 0,
                            finetune_data: fineTuneInfo,
                        });
                        updEnt.updateToDB(function(error){
                            if(error){
                                return callback(error, null);
                            }
                            return callback(error, fineTuneInfo,data);
                        });
                    }
                }
            });

        });

    }
    /**获取模板的提纲微调信息
     * 参数: typeL	0 未分类   1 海报  2 多页  3 ppt. 4, 印品, 6, 社交媒体
     * 参数: designId	模板 id
     * 返回结果: callback,参数如下
     *          error：null 表示无错误
     *          data：结构如下：
     *               {
     *                 "bleedHeight": 36,
     *                 "bleedWidth": 36,
     *                 "coreStructure": {},            // 文档核心结构字符串
     *                 "designId": 738,
     *                 "designNm": "红色线型简约名片",
     *                 "pageCount": 2,
     *                 "pageHeight": 709,
     *                 "pageWidth": 1134,
     *                 "printPxW": 1200, // 印刷用像素值
     *                 "printPxH": 1900, // 印刷用像素值
     *                 "productId": 0,
     *                 "productNm": "",
     *                 "textObjects": “",               //  微调初始化字符串
     *                 "typeL": 4
     *               }
     *
     * */
    tg3Util.getTgAndFineTuneInfoOfDesign = function  (typeL,designId, callback) {
        var entity = new TgDesignDocModel({
            design_id:designId,
            del_flg:0
        });

        entity.getTgAndFineTuneInfoOfDesign(function(error, data){
            if(!data){
                return callback(error,null);
            }else{
                // 设置typeL
                data.typeL = typeL;

                // 查看微调数据，如果存在则返回，
                if(data.textObjects && data.textObjects.length>0){
                    return callback(null ,data);
                }else{
                    // 如果微调数据不存在，则创建并更新到design中
                    //tg3Util.createFineTuneInfoOfDesign(designId,  function(error, fineTuneInfo, coreStructure){
                    tg3Util.createFineTuneInfoOfDesignByHTTP(designId,  function(error, fineTuneInfo, coreStructure){// replace by jackLiu20180505 使用image服务来处理
                        if(!fineTuneInfo){
                            console.log('无法根据模版创建微调数据');
                            return callback('无法根据模版创建微调数据', null);
                        }else{
                            // 设置微调数据
                            data.textObjects = fineTuneInfo;
                            data.coreStructure = coreStructure;
                            return callback(null ,data);
                        }
                    });
                }
            }
        });
    }
    /**获取作品的提纲微调信息
     * 参数: typeL	0 未分类   1 海报  2 多页  3 ppt. 4, 印品, 6, 社交媒体
     * 参数: docId	 对应用户文档 id
     * 返回结果: callback,参数如下
     *          error：null 表示无错误
     *          data：结构如下：
     *               {
     *                 "bleedHeight": 36,
     *                 "bleedWidth": 36,
     *                 "coreStructure": {},            // 文档核心结构字符串
     *                 "designId": 738,
     *                 "designNm": "红色线型简约名片",
     *                 "pageCount": 2,
     *                 "pageHeight": 709,
     *                 "pageWidth": 1134,
     *                 "printPxW": 1200, // 印刷用像素值
     *                 "printPxH": 1900, // 印刷用像素值
     *                 "productId": 0,
     *                 "productNm": "",
     *                 "textObjects": “",               //  微调初始化字符串
     *                 "typeL": 4
     *               }
     *
     * */
    tg3Util.getTgAndFineTuneInfoOfUserDoc = function  (typeL,docId, callback) {
        var entity = new TgUserDocModel({
            doc_id:docId,
            del_flg:0
        });

        entity.getTgAndFineTuneInfoOfUserDoc(function(error, data) {
            if (!data) {
                return callback(error, null);
            } else {
                // 设置typeL
                data.typeL = typeL;

                // 查看微调数据，如果存在则返回，
                if(data.textObjects && data.textObjects.length>0){
                    return callback(null ,data);
                }else{
                    //----------------以下为保护代码，防止用户文档微调不存在的情况-----------------
                    // 如果微调数据不存在，则获取其design的微调数据
                    var entity2 = new TgDesignDocModel({
                        design_id:data.designId,
                        del_flg:0
                    });
                    entity2.findFromDB(function(error,items) {
                        if (error|| !items || items.length==0) {
                            console.log(error);
                            return callback(error,null);
                        }
                        var designDoc = items[0];
                        if(designDoc.finetune_data && designDoc.finetune_data.length>0){
                            // 设置微调数据
                            data.textObjects = designDoc.finetune_data;
                            return callback(null ,data);
                        }else{
                            // 如果design微调数据不存在，则创建并更新到design中
                            // tg3Util.createFineTuneInfoOfDesign(data.designId,  function(error, fineTuneInfo, coreStructure){
                            tg3Util.createFineTuneInfoOfDesignByHTTP(data.designId,  function(error, fineTuneInfo, coreStructure){// replace by jackLiu20180505 使用image服务来处理
                                if(!fineTuneInfo){
                                    console.log('无法根据模版创建微调数据');
                                    return callback('无法根据模版创建微调数据', null);
                                }else{
                                    // 设置微调数据
                                    data.textObjects = fineTuneInfo;
                                    data.coreStructure = coreStructure;
                                    return callback(null ,data);
                                }
                            });
                        }

                    },0,1);
                }
            }
        });
    }

    // 删除切换模版记录
    tg3Util.doClrHis = function (docId,callback){
        return callback(null, null);
        var url = CONFIG.SWITCH_SERVER_DOMAIN +':'+CONFIG.SWITCH_SERVER_PORT+'/tgSwitch/clrSwitchHis?'
            +'docId='+docId;

        superagent.get(url)
            .end(function(err, res){
                if(err){
                    console.log('tg.js [saveUserDocOnly] SuperAgent clrSwitchHis Err: ' + err);
                    return callback(err, null);
                }

                var retJson = JSON.parse(res.text);
                if(retJson.success){
                    return callback(null,retJson.data);

                } else {
                    return callback(retJson.msg, null);
                }
            }
        );
    }

    /**保存提纲微调信息
     * 参数: userId	用户唯一标识
     * 参数: doc	对应提纲微调信息 json 字符串
     *              "{
     *                  "coreStructure": {},            // 文档核心结构对象
     *                  "designId": 738,                // 更新时不需要传递, 新建时需要
     *                  "productId": 0,                  // 为0 标识模板, 保存时新建, 不为零时标识作品, 保存时仅更新
     *                  "productNm": "",
     *                  "textObjects": “",               //  微调初始化字符串
     *                  “tag” : [1]                          // 提纲特征值
     *              }"
     * 参数: clrHis	是否删除切换模版历史记录(等于1时删除,默认不删除)
     * 参数: typeL	0 未分类   1 海报  2 多页  3 ppt. 4, 印品, 6, 社交媒体
     * 参数  skipUserChk[可选]: 是否检查用户(等于1时不检查,默认检查)
     * 返回结果:	callback，参数如下：
     *              error:
     *              data:{
     *                  "productId": 12345
     *              }
     * */
    tg3Util.saveTgAndFineTuneInfo_v5_3 = function  (userId, doc, clrHis, typeL,skipUserChk, cb) {
        // proc
        async.parallel({
            getDesign: function(callback){
                // 检索design
                var entityD = new TgDesignDocModel({
                    design_id: doc.designId,
                    del_flg: 0
                });
                entityD.findFromDB(function(err, items) {
                    if (err) {
                        console.log('tg.js [saveTgAndFineTuneInfo_v5_3] Check design_id Err: ' + err);
                        return callback(err, null);
                    }

                    if (items != null && items.length > 0) {
                        return callback(null, items[0]);

                    } else {
                        console.log('tg.js [saveTgAndFineTuneInfo_v5_3] design_id invalid ');
                        return callback("无效DESIGN_ID", null);
                    }
                });
            },

            getUser: function(callback){
                if(skipUserChk == 1){
                    // 直接返回
                    return callback(null, {user_id: userId, user_type:1});
                } else {
                    // 检索user
                    var entity = new ItbUserModel({
                        //uuid    : uuid,
                        user_id : userId,
                        del_flg : 0
                    });
                    entity.findFromDB(function(err, items){
                        if (err){
                            console.log('tg.js [saveTgAndFineTuneInfo_v5_3] Check user_id Err: '+err);
                            return callback(error, null);
                        }

                        if (items != null && items.length > 0) {
                            return callback(null, items[0]);

                        } else {
                            console.log('tg.js [saveTgAndFineTuneInfo_v5_3] user_id invalid ');
                            return callback(CODE.USER_ID_INVAILD, null);
                        }
                    });
                }
            }

        }, function(error, result){
            if (error) {
                console.log(error);
                return cb(error, null);
            }

            var userItem = result.getUser;
            var designItem = result.getDesign;

            if (doc.productId=='undefined' || doc.productId==null || Number(doc.productId)==0) {
                // 新规
                var newDocEntity = new TgUserDocModel({
                    tg_id: designItem.tg_id,
                    map_id: designItem.map_id,
                    design_id: designItem.design_id,
                    doc_name: doc.productNm || '我的作品',// doc.coreStructure.name
                    user_id: userItem.user_id,
                    user_type: userItem.user_type,
                    purpose: 0,
                    size_id: designItem.size_id,
                    type_id: designItem.type_id,
                    data: JSON.stringify(doc.coreStructure) || '',
                    hash_tag: JSON.stringify(doc.tag) || '[]',// TODO
                    finetune_data:doc.textObjects,
                    finetune_flg:1,
                    ver: doc.coreStructure.version || 0,
                    update_by: userItem.user_id,
                });
                // insert document
                newDocEntity.saveToDB(function(err, newDocId){
                    if (err) {
                        console.log('tg.js [saveUserDocOnly] Ins Doc Err: '+err);
                        return cb(err,null);
                    }

                    var retData = {
                        designId: designItem.design_id,
                        productId: newDocId,
                    };

                    return cb(null, retData);
                });

            } else {
                // 更新doc
                var updDocEnt = new TgUserDocModel({
                    doc_id: doc.productId,// Key
                    doc_name: doc.productNm || '我的作品',// doc.coreStructure.name
                    data: JSON.stringify(doc.coreStructure) || '',
                    hash_tag: JSON.stringify(doc.tag) || '[]',// TODO
                    finetune_data:doc.textObjects,
                    finetune_flg:1,
                    ver: doc.coreStructure.version || 0,
                    update_by: userItem.user_id,
                });
                updDocEnt.updateToDB(function(err){
                    if (err) {
                        console.log('tg.js [saveUserDocOnly(upd)] Upd Doc Err: '+err);
                        return cb(err, null)
                    }

                    // 逻辑删除page
                    var delPgEnt = new TgUserPageModel({
                        doc_id: doc.productId,
                        update_by: userItem.user_id,
                    });
                    delPgEnt.delPageLogic(function(delErr){
                        if (delErr) {
                            console.log('tg.js [saveUserDocOnly(upd)] Del Pages Err: ' + delErr);
                            return cb(delErr,null);
                        }

                        // 逻辑删除pc
                        var delPcEnt = new TgUserPcModel({
                            doc_id: doc.productId,
                            update_by: userItem.user_id,
                        });
                        delPcEnt.delPcLogic(function(delErr2){
                            if (delErr2) {
                                console.log('tg.js [saveUserDocOnly(upd)] Del PCs Err: ' + delErr2);
                                return cb(delErr2,null);
                            }

                            // 返回值
                            var retData = {
                                designId: designItem.design_id,
                                productId: doc.productId,
                            };

                            if(1==clrHis){
                                // 清除切换历史
                                tg3Util.doClrHis(doc.productId, function(clrErr, clrRet){
                                    if (clrErr) {
                                        console.log('tg.js [saveUserDocOnly(upd)] doClrHis Err: '+clrErr);
                                        return cb(clrErr,null);
                                    }
                                    return cb(null,retData);
                                });

                            } else {
                                return cb(null,retData);
                            }
                        });
                    });
                });
            }
        });
    }

    //------------------------------------------------------------------
    /* 根据指定标签数组 和 对应条目信息, 获取指定模板
     * 方式:GET/POST
     * 参数 optionalIds: 条目选择界面的所有选中的 可选条目对应节点的 id 数组, 例如：[45, 78, 99]
     * 参数 tgId:  条目选择获取的 tgId
     * 参数 withFineTune： 1表示返回微调数据
     * 返回:如果withFineTune为0，则返回下列数据：{
     *                      "productId":0,
     *                      "designId":321,
     *                      "coreStructure":{"version":3,"name":"会议邀请函","root":{}}
     *                }
     *                如果withFineTune为1，则返回下列数据：{
     *                      "bleedHeight": 36,
     *                      "bleedWidth": 36,
     *                      "coreStructure": {},            // 文档核心结构对象
     *                      "designId": 738,
     *                      "designNm": "红色线型简约名片",
     *                      "pageCount": 2,
     *                      "pageHeight": 709,
     *                      "pageWidth": 1134,
     *                      "productId": 0,
     *                      "productNm": "",
     *                      "textObjects": “",               //  微调初始化字符串
     *                      "typeL": 4
     *                 }
     *}
     * 测试地址：http://localhost:3000/tg3/getDesignByItem?tgId=117&optionalIds=[7997585496]
     * */
    tg3Util.getDesignByItem = function(optionalIds, tgId, withFineTune , callback){
        // 获取题纲的数据
        tg3Util.getTgTreeById(tgId,function (error, data) {
            if (error) {
                console.log('tg3Util.js getDesignByItem error: '+error);
                return callback(error, null);
            }
            var tree= null;
            if(data.tree.version==SJGNode.VERSION_3){
                tree =new SJGTGV3();
                tree.fromJson(data.tree,data.tree.version);
            }else{
                tree =new SJGTG();
                tree.fromJson(data.tree,data.tree.version);
            }

            // 更新可选节点的隐藏属性、计算特征值、并根据特征值获取某个分页映射下的设计
            tg3Util.showTgTreeNode(tree, optionalIds, 1);
            var eigen = tree.calcEigenValue();
            tg3Util.getDesignByEigenOfHidden(tgId, eigen, withFineTune ,function(error, data){
                if (error) {
                    console.log('tg3Util.js getDesignByItem error: '+error);
                    return callback(error, null);
                }

                if(data){
                    // 隐藏未选中的可选节点
                    tg3Util.showAndHideTgTreeOptionNode(data.coreStructure, optionalIds);
                    return callback(null, data);
                }

                // 模糊匹配
                // 如果没有特征值匹配的设计，则将可选节点设置为全部显示，计算特征值、并根据特征值获取某个分页映射下的设计
                tg3Util.adjustTgTreeForFuzzyMatching(tree);
                var eigen = tree.calcEigenValue();
                tg3Util.getDesignByEigen(tgId, eigen, withFineTune ,function(error, data){
                    if (error) {
                        console.log('tg3Util.js getDesignByItem error: '+error);
                        return callback(error, null);
                    }

                    if(data){
                        // 隐藏未选中的可选节点
                        // 例如:有4个可选节点，快速创建只选了3个，需要把剩下1个隐藏
                        // *前提是该分类的提纲下，至少有一个模版的所有可选项目均已加入模版
                        tg3Util.showAndHideTgTreeOptionNode(data.coreStructure, optionalIds);
                        return callback(null, data);
                    }

                    // 返回失败
                    return callback(null, null);
                });
            });
        });

    };

    // 搜索
    tg3Util.get_search_v5_3 = function (clientVer, withTotalPage, keyword, typeLs, pageNum, pageSize, callback){
        // 是否在返回值中包含vip模版
        var includeVipTemplate = 1;

        // 处理搜索词
        var arr = keyword.split(",");
        var segArr = [];
        for(var i=0;i<arr.length;i++){
            var s = tg3Util.doSegmentOfText(arr[i]);
            if(s){
                segArr = segArr.concat(s);
            }
        }
        var segKeyword = segArr.join(",");

        // 处理搜索一级分类
        var arrTypeL = JSON.parse(typeLs||'[]');
        // 屏蔽当前不支持的分类
        var realTypeLs = [];
        for(var index in arrTypeL) {
            var typeL = Number(arrTypeL[index]) || 0;
            // 非当前支持类型, 不支持搜索
            if( typeL != CONFIG.DOC_TYPE_1 &&
                typeL != CONFIG.DOC_TYPE_2 &&
                typeL != CONFIG.DOC_TYPE_3 &&
                typeL != CONFIG.DOC_TYPE_4 &&
                typeL != CONFIG.DOC_TYPE_6 &&
                typeL != CONFIG.DOC_TYPE_7 &&
                typeL != CONFIG.DOC_TYPE_8)
            {
                continue;
            }
            realTypeLs.push(typeL);
        }
        // 拼接搜索一级分类参数
        arrTypeL = realTypeLs;
        var pTypeLs = '';
        if(arrTypeL && arrTypeL.length>0) {
            pTypeLs = arrTypeL.join(',');
        }
        pTypeLs = '[' + pTypeLs + ']';

        // 处理搜索二级分类
        var arrTypeM = [];
        // 5.2.0
        if(clientVer == "5.2.0"){
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];

                // 印品的特殊处理, 5.2.0 限制名片, 传单, 展架
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300');
                } else {
                    arrTypeM.push('0');
                }
            }
        }
        // 5.3.0
        else if(clientVer == "5.3.0") {
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 5.3.0 限制名片, 传单, 展架
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300');
                }
                // 社交媒体的特殊处理, 5.3.0 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL == CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                }else {
                    arrTypeM.push('0');
                }
            }
        }
        // 5.5
        else if((clientVer||'').substr(0,3) == "5.5") {
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 5.5 限制名片, 传单, 展架, 招贴海报
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300_40500');
                }
                // 社交媒体的特殊处理, 5.5.0 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL==CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                }else {
                    arrTypeM.push('0');
                }
            }
        }
        // 5.6
        else if((clientVer||'').substr(0,3) == "5.6") {
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 限制名片, 传单, 展架, 招贴海报, 优惠券, 不干胶
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300_40500_40600_40700');
                }
                // 社交媒体的特殊处理, 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL==CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                }else {
                    arrTypeM.push('0');
                }
            }
        }
        // 5.7
        else if((clientVer||'').substr(0,3) == "5.7") {
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 限制名片, 传单, 展架, 招贴海报, 优惠券, 不干胶, 停车牌
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300_40500_40600_40700_40800');
                }
                // 社交媒体的特殊处理, 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL==CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                } // 电商的特殊处理, 限制淘宝主图, 淘宝banner
                else if(typeL==CONFIG.DOC_TYPE_7) {
                    arrTypeM.push('70100_70200');
                }else {
                    arrTypeM.push('0');
                }
            }
        }
        // >= 6.0
        else if( Number((clientVer||'').substr(0,3)) >= Number("6.0")) {
            includeVipTemplate =  1;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 限制名片, 传单, 展架, 招贴海报, 优惠券, 不干胶, 停车牌
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300_40500_40600_40700_40800');
                }
                // 社交媒体的特殊处理, 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL==CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                } // 电商的特殊处理, 限制淘宝主图, 淘宝banner
                else if(typeL==CONFIG.DOC_TYPE_7) {
                    arrTypeM.push('70100_70200');
                } // 微商的特殊处理, 限制产品,代理,微商,节日,课程,活动,红包,对比,通知,截图
                else if(typeL==CONFIG.DOC_TYPE_8) {
                    arrTypeM.push('80100_80200_80300_80400_80500_80600_80700_80800_80900_81000');
                }else {
                    arrTypeM.push('0');
                }
            }
        }
        else {
            includeVipTemplate =  0;

            for(var i = 0; i < arrTypeL.length; i ++) {
                var typeL = arrTypeL[i];
                // 印品的特殊处理, 5.3.0 限制名片, 传单, 展架
                if(typeL==CONFIG.DOC_TYPE_4){
                    arrTypeM.push('40100_40200_40300');
                }
                // 社交媒体的特殊处理, 5.3.0 限制微信封面大图, 微信封面小图, 微博封面
                else if(typeL==CONFIG.DOC_TYPE_6) {
                    arrTypeM.push('60100_60200_60300');
                }else {
                    arrTypeM.push('0');
                }
            }
        }

        var pTypeMs = '';
        if(arrTypeM && arrTypeM.length>0) {
            pTypeMs = arrTypeM.join(',');
        }
        pTypeMs = '[' + pTypeMs + ']';

        // 搜索
        var entity = new TgKeywordModel();
        entity.search_v5_3(segKeyword, pTypeLs, pTypeMs, pageNum, pageSize, withTotalPage, includeVipTemplate, callback);
    };

    // 获取专题列表(按时间排序)
    tg3Util.getSpecialTopics_v5_5=function(pageNum, pageSize, callback) {
        var ent  = new TgSpecialTopicModel({
            use: 2,// 专题
            publish: 1,// 已发布
            del_flg: 0,
            //order_by: "update_time"
        });
        ent.findSpecTopicWithType(function(err, ret){
            if(err) {
                return callback(err, null);
            }

            var retData=[];
            if(ret&&ret.length>0) {
                for(var i=0;i<ret.length;i++) {
                    retData.push({
                        id: ret[i].st_id,
                        thumb: ret[i].thumb||'',
                        title: ret[i].title||'',
                        typeL: ret[i].top_type,
                        h: 350,// TODO
                        w: 710// TODO
                    });
                }
            }
            return callback(null, retData);
        },pageNum*pageSize, pageSize);
    };

    // 获取首页用热门专题列表(按ordery排序)
    tg3Util.get_hotsSpecialTopics_v5_5=function(callback) {
        var ent  = new TgSpecialTopicModel({
            use: 2,// 专题
            publish: 1,// 已发布
            del_flg: 0,
        });
        ent.findSpecTopicWithType(function(err, ret){
            if(err) {
                return callback(err, null);
            }

            var retData=[]
            if(ret&&ret.length>0) {
                for(var i=0;i<ret.length;i++) {
                    retData.push({
                        id: ret[i].st_id,
                        thumb: ret[i].thumb||'',
                        title: ret[i].title||'',
                        typeL: ret[i].top_type,
                        h: 440,// TODO
                        w: 750// TODO
                    });
                }
            }
            // 首页热门专题数据结构组织
            var data = {
                title: "热门专题",
                en: CONFIG.CHS_EN.getEn('热门专题',1),
                column: 2,
                direction: 1,
                list: retData || []
            };
            return callback(null, data);
        },0, 5);// TODO 暂时固定取前5个
    };

    //获取首页秒杀图片信息
    tg3Util.get_seckillInfo_v6_1 = function(callback) {
        var retData = {
            thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/12/181443239_4IUyR8ej9eoU2UXyVehR",// [2018-03-12, new]
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180404226_1sKI9lsBBl2jBbKML6Fv",// [2018-04-26,五一]
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/28/153053132_S2dW0UW8qPqQUlmAhLXh",
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2017/08/18/172559145_mrPv0hcm2zKUQJKbvZ6S",// old
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/01/31/185651911_W43yp033vWLATnErMplQ",// 新年
            w: "350",
            h: "200"
        };
        return callback(null, retData);
    };

    //获取首页热门专题图片信息
    tg3Util.get_specialTopicInfo_v6_1 = function(callback) {
        var retData = {
            thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/03/13/184016592_xRhfOXKCX3LvsQKRKyn6",// [2018-03-13, new],
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180404673_hPX7ReJG0QB5DN3ius7o",// [2018-04-26, 五一]
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/28/153053535_AwLzW0huy2DMBFRHN6f0",
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/12/181444280_M9LYBfAvnXSGG37Jc9C4",// [2018-03-12]
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2017/08/18/172558844_VCj5t5LnG4Y6JSDtKyCL",// old
            //thumb: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/01/31/185651600_RJzKjNtzCBKFqF9ZNg9o",// 新年
            w: "350",
            h: "200"
        };
        return callback(null, retData);
    };

    /**
     * 场景列表
     * @param callback
     * {
     *  "column": 1,
     *  "direction": 0,
     *  "en": "SCENCE",
     *  "icon": "http://oss-itbour-special-topic-icon.png",
     *  "hiddenMore" : 1,
     *  "list": [{
     *     "h": 204,
     *     "w": 344,
     *     "id": 982,
     *     "thumb": "http://itbourgenerate.osscnhangzhou.aliyuncs.com/image/0.jpg",
     *     "url" : "http://www.itbour.com/scene/yi-jia-si-shi-nian-de-lao-dian.html",
     *     "title": "一家四十年的老店",
     *     "desc" : "浦沿*新生村....."
     *  }],
     *  "title": "场景案例"
     * }
     */
    tg3Util.get_sceneList_v5_6 = function(callback){
        (new ItbSceneModel).findScenceItemsFromDB(function(err, ret){
            if(err) {
                return callback(err, null);
            }

            var retData = {
                column: 1,
                direction: 1,
                title: "场景案例",
                en: CONFIG.CHS_EN.getEn('场景案例',1),
                icon: '',// TODO
                hiddenMore: 0,
                list: [],
            };
            if(ret && ret.length>0) {
                for(var i=0; i<ret.length; i++) {
                    retData.list.push({
                        w: 260,
                        h: 180,
                        id: ret[i].s_id,
                        thumb: ret[i].image || '',
                        url: ret[i].url || '',
                        title: ret[i].title || '',
                        desc: ret[i].desc || ''
                    });
                }
            }
            retData.list = retData.list.slice(0,3); // 限定3个场景案例
            return callback(null, retData);
        },0,12);
    };

    /**
     * 首页快速入口
     * @param callback
     * [
     *   {
     *     "icon":"http://itbourgenerate.osscnhangzhou.aliyuncs.com/image/0.jpg",
     *     "title":"名片",
     *     "typeL":4,
     *     "typeM":40100
     *   }
     * ]
     */
    tg3Util.get_fastTrack_v5_6 = function(callback){
        var retData = [
            {
                typeL: 4,
                typeM: 40100,
                title: "名片",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930210_lbeua9IeBWTUVdnR7qPG",
            },
            {
                typeL: 4,
                typeM: 40600,
                title: "优惠券",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164929956_zeHBYVH3KYcIPZQAl5pt",
            },
            {
                typeL: 4,
                typeM: 40700,
                title: "不干胶",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930294_uu3x38wcnj4yJic6XxI7",
            },
            //{
            //    typeL: 1,
            //    typeM: 60,
            //    title: "电子名片",
            //    icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/20/17402066_LIt6Fw3dtefLzQR74FVs",
            //},
            {
                typeL: 1,
                typeM: 25,
                title: "微商",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930252_5CYnB3Iulg03wxaeKnZP",
            },
        ];
        return callback(null, retData);
    };

    // 获取文字可调颜色列表
    // 返回值: 二维数组 [[r,g,b]]
    tg3Util.getTextColors_v5_6=function(callback) {
        var ret = [
            [255,255,255], [254,228,227], [252,204,156], [204,254,203], [203,152,253], [102,204,254],
            [192,192,192], [213,101,100], [254,203,0],   [100,255,99],  [152,51,253],  [49,152,253],
            [128,128,128], [152,51,0],    [254,153,0],   [151,204,0],   [101,0,202],   [0,0,253],
            [0,0,0],       [128,0,0],     [254,103,0],   [0,128,0],     [50,0,100],    [0,0,127],
        ];
        return callback(null, ret);
    };

    //------------------------------------------------------------------
    // 获取电商分类
    tg3Util.get_eBusinessTypeM_v5_7 = function ( callback){
        var typeL = 7;//分类: 1单页, 2多页, 6社交媒体, 7 电商;

        var data = [
            {
                id : 70200,
                name : "淘宝banner",
                en: CONFIG.CHS_EN.getEn('淘宝banner',1),
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/07/11/192835140_W0ONMAk2SeO5eAf8eQ5Y'
            },
            {
                id : 70100,
                name : "淘宝主图",
                en: CONFIG.CHS_EN.getEn('淘宝主图',1),
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/07/11/192835281_ja2RDFbyikdC7tR4Nhum'
            }
        ];
        return callback(null,data);
    };

    //----------------------------v5.7 热门系列--------------------------------------
    /*
    参数：type：社交媒体的二级分类
    * 参数：pageNum：页码
    * 参数：pageSize：
    * 参数：callback：回调，参数如下：
    *          error:错误消息
    *          data:获取专题的内容列表的数组，数组元素如下：
    *                 id: 模板id
    *                 thumb:
    *                 typeL:
    *                 title: 模板名称
    *                 desc: 价格描述
    *                 w:
    *                 h:
    *
    */
    tg3Util.getHotTopicItemsOfEBusinessTypeM = function(type,pageNum,pageSize,callback) {
        var entity = new TgSpecialTopicModel({
        });

        var skipNum = pageNum*pageSize;
        entity.findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                adInfo.push({
                    id:items[i].item_id,
                    thumb:items[i].thumb,
                    typeL: 7, // 社交媒体
                    title: items[i].tmpl_name||'',
                    desc: items[i].tmpl_price||'',
                    w:items[i].width,
                    h:items[i].height,
                    useCount: items[i].show_apply_cnt||items[i].apply_cnt||0
                });
            }
            return callback(null, adInfo);
        },type,skipNum,pageSize);
    };
    // 热门淘宝主图
    tg3Util.get_hotsTaobaoMainPhoto_v5_7 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var type=70100;// (电商/淘宝主图)
        this.getHotTopicItemsOfEBusinessTypeM(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "淘宝主图",
                en: CONFIG.CHS_EN.getEn('淘宝主图',1),
                typeL: 7,
                typeM: 70100,
                column: 3,
                direction: 0,
                list: data || []
            };
            return callback(error,data);
        });
    };

    // 热门淘宝 banner
    tg3Util.get_hotsTaobaoBanner_v5_7 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var type=70200;// (电商/淘宝 banner)
        this.getHotTopicItemsOfEBusinessTypeM(type,pageNum, pageSize,function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "淘宝banner",
                en: CONFIG.CHS_EN.getEn('淘宝banner',1),
                typeL: 7,
                typeM: 70200,
                column: 1,
                direction: 0,
                list: data || []
            };
            return callback(error,data);
        });
    };

    // 热门停车牌
    tg3Util.get_hotsStopSign_v5_7 = function(callback) {

        var pageSize = 12;
        var pageNum = 0;
        var type=40800; // 停车牌
        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            var data = {
                title: "停车牌",
                en: CONFIG.CHS_EN.getEn('停车牌',1),
                typeL: 4,
                typeM: 40800,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门优惠券
    tg3Util.get_hotsPrintCoupon_v5_7 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var type=40600; // 优惠券
        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            var data = {
                title: "优惠券",
                en: CONFIG.CHS_EN.getEn('优惠券',1),
                typeL: 4,
                typeM: 40600,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门不干胶
    tg3Util.get_hotsPrintSticker_v5_7 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var type=40700; // 不干胶
        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            var data = {
                title: "不干胶",
                en: CONFIG.CHS_EN.getEn('不干胶',1),
                typeL: 4,
                typeM: 40700,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门招贴海报
    tg3Util.get_hotsPrintPoster_v5_7 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var type=40500; // 招贴海报-->海报
        this.getHotTopicItemsOfPrintTypeM( type,pageNum,pageSize,function(error,data) {
            var data = {
                title: "海报",
                en: CONFIG.CHS_EN.getEn('海报',1),
                typeL: 4,
                typeM: 40500,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    //------------------------------------------------------------------------------------
    /**
     * 跟据模板 id, 获取文字索引信息
     *
     * @param designId 模板唯一标识
     * @param callback 回调
     *              error 若出错, 出错原因
     *              texts 模板对应文字轮廓信息
     * */
    tg3Util.getTextInfoByDesignId = function(designId, callback){

        function getPageTexts(pc, ver) {
            var mobileZoom = 50;
            var ret = [];
            var globalData = new GlobalData();
            pc.data = pc.data.replace(/\n/g,'\\n').replace(/\t/g,'').replace(/\r/g,'');//.replace(/\\"/g,'\\"');
            pc.data = pc.data.replace(/[\x00-\x09\x0B-\x1F\x7F-\x9F]/g, "");//  remove control characters from string
            var page = new SJGPage(globalData);
            page.fromJson(JSON.parse(pc.data), ver);
            /**
             * [{id:1,fz:64,te:\"xyz\":style:{ v:1,fn:微软雅黑,fs:20,bi:0,po:1,vpo:2,cs:6,ls:10,td:0} }]
             * */
            var texts = page.root.getChildObjOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextExtObj, false, true);
            if(texts && texts.length>0){
                for(var j=0;j<texts.length;j++){
                    var t = texts[j];
                    t.style.fs *= 50;
                    if(t && t.style && t.style.fn){

                        ret.push({
                            id: t.id,
                            fz: t.fz,
                            te: t.te,
                            style: t.style
                        });
                    }
                }
            }else{
                var texts = page.root.getChildObjOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj, false, true);
                for(var j=0;j<texts.length;j++){
                    var t = texts[j];
                    t.style.fs *= 50;
                    if(t && t.fn){
                        ret.push({
                            id: t.id,
                            fz: t.fz,
                            te: t.te,
                            style: t.style
                        });
                    }
                }
            }
            return ret;
        }
        // 获取文档版本, pc的data和 ver信息
        var designPc = new TgDesignPcModel({
            design_id:designId,
            del_flg : 0
        });
        designPc.getPcDataOfDesign(function(error,results){
            if(error) {
                return callback(error);
            }
            if(!results || results.length == 0) {
                return callback('文档不存在');
            }
            var texts = [];
            for (var i = 0; i < results.length; i++) {
                var t = getPageTexts(results[i], results[i].ver);
                texts.push(t);
            }
            callback(null, texts);
        });
    };

    // =================================================== v6.0新增 ===================================================
    /**
     * 首页快速入口
     * @param callback
     * [
     *   {
     *     "icon":"http://itbourgenerate.osscnhangzhou.aliyuncs.com/image/0.jpg",
     *     "title":"名片",
     *     "typeL":4,
     *     "typeM":40100
     *   }
     * ]
     */
    tg3Util.get_fastTrack_v6_0 = function(callback){
        var retData = [
            {
                typeL: 4,
                typeM: 40100,
                title: "名片",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930210_lbeua9IeBWTUVdnR7qPG",
            },
            {
                typeL: 4,
                typeM: 40600,
                title: "优惠券",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164929956_zeHBYVH3KYcIPZQAl5pt",
            },
            {
                typeL: 4,
                typeM: 40800,
                title: "停车牌",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/07/25/104226214_qZHxrjFwm8aDlUbBeiQt",
            },
            {
                typeL: 1,
                typeM: 25,
                title: "微商",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930252_5CYnB3Iulg03wxaeKnZP",
            },
        ];
        return callback(null, retData);
    };

    tg3Util.get_fastTrack_v6_1 = function(callback){
        var retData = [
            {
                typeL: 4,
                typeM: 40100,
                title: "名片",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/13/102458879_9qgkzn4UEiH4EfwaQ2O0",// [2018-03-13]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180405702_sELhJwr8NruELc5779hH",// [2018-04-26, 五一]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930210_lbeua9IeBWTUVdnR7qPG",// old
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/02/01/170903107_L2NTcx5WHd0IsqDAbais"// 新年
            },
            {
                typeL: 4,
                typeM: 40600,
                title: "优惠券",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/13/102458754_d0qIolukX2KmbfABfM47",// [2018-03-13]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180405591_BQUNaQM847tkJ81UKZaU",// [2018-04-26, 五一]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164929956_zeHBYVH3KYcIPZQAl5pt",// old
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/02/01/1709031_QZQCtL7UohTlQDNHyQGB"// 新年
            },
            {
                typeL: 4,
                typeM: 40800,
                title: "停车牌",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/13/102458803_e4x4gUw7KboB0FhbFzjN",// [2018-03-13]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180405411_qYIsL4dwXgaKQA5MTWmt",// [2018-04-26, 五一]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/07/25/104226214_qZHxrjFwm8aDlUbBeiQt",// old
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/02/01/170902920_HjIS9MSuzQFEN17SZ0bv"// 新年
            },
            {
                typeL: 8,
                typeM: -1,
                title: "微商",
                icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/03/13/102458572_64yrJ0BjN503aohI6RFh",// [2018-03-13]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/04/26/180405315_uJukWRliGyVUCWVHRyRH",// [2018-04-26, 五一]
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/29/164930252_5CYnB3Iulg03wxaeKnZP",// old
                //icon: "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2018/02/01/170902704_6EjdWpQbxB35WJwXQRP7"// 新年
            },
        ];
        return callback(null, retData);
    };

    //-------------------------------------------------- v6.0 热门部分 -----------------------------------------------------
    /***
     * 获取热门设计/商品项目核心方法
     * @param typeL 指定一级分类
     * @param type 指定热门的分类
     * @param pageNum 指定页码
     * @param pageSize 指定页容量
     * @param w 强制指定缩略图宽
     * @param h 强制指定缩略图高
     * @param callback 回调
     *          error 若查询出错, 错误原因
     *          arr 查询结果数组
     */
    tg3Util.getHotItemCore_v6_0 = function (typeL, type, pageNum, pageSize,  w, h, callback){
        var entity = new TgSpecialTopicModel({
        });

        var skipNum = pageNum*pageSize;
        entity.findHotRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var arr = [];
            for(var i=0;i<items.length;i++){
                var obj = {
                    id: items[i].goods_id,
                    thumb: items[i].thumb||'',
                    typeL: typeL,
                    title: items[i].goods_name||'',
                    desc: items[i].goods_price||'',
                    w: w || items[i].width,
                    h: h || items[i].height,
                    useCount: items[i].show_apply_cnt||items[i].apply_cnt||0,
                    vip : items[i].vip
                };
                if(typeL != 4) {
                    obj.id = items[i].item_id;
                    obj.title = items[i].tmpl_name||'';
                    obj.desc = items[i].tmpl_price||'';
                }
                // vip 的描述更新为 VIP 专享 add by wangjun 2017-10-27
                if(obj.vip == 10) {
                    obj.desc = 'VIP专享'
                }
                arr.push(obj);
            }
            return callback(null, arr);
        },type,skipNum,pageSize, true);
    };

    // 热门单页
    tg3Util.get_hotsPoster_v6_0 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var type=1;// 分类: 1单页, 2多页, 3ppt
        this.getHotItemCore_v6_0(type, type, pageNum, pageSize, null, null, function(error, data){
            // 热门与列表显示列数不同
            data = {
                title: "单页",
                en: CONFIG.CHS_EN.getEn('单页',1),
                typeL: 1,
                typeM: -1,
                column: 3.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });

    };

    // 热门微信大图
    tg3Util.get_hotsWechatCoverBigPic_v6_0 = function (callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 6;
        var typeM = 60100;
        this.getHotItemCore_v6_0(typeL,typeM, pageNum, pageSize, null, null,function(error, data){
            // 热门与列表显示列数不同
            data = {
                title: "公众号首图",
                en: CONFIG.CHS_EN.getEn('公众号首图',1),
                typeL: typeL,
                typeM: typeM,
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(error,data);

        });
    };

    // 热门微信小图
    tg3Util.get_hotsWechatCoverSmallPic_v6_0 = function ( callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 6;
        var typeM = 60200;
        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, null, null, function(error, data){
            // 热门与列表显示列数不同
            data = {
                title: "公众号小图",
                en: CONFIG.CHS_EN.getEn('公众号小图',1),
                typeL: typeL,
                typeM: typeM,
                column: 3.2,
                direction: 0,
                list: data
            };
            return callback(error,data);
        });
    };

    // 热门淘宝主图
    tg3Util.get_hotsTaobaoMainPhoto_v6_0 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 7;
        var typeM = 70100; // 电商, 淘宝主图

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, null, null, function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "淘宝主图",
                en: CONFIG.CHS_EN.getEn('淘宝主图',1),
                typeL: typeL,
                typeM: typeM,
                column: 3,
                direction: 0,
                list: data || []
            };
            return callback(error,data);
        });
    };

    // 热门淘宝 banner
    tg3Util.get_hotsTaobaoBanner_v6_0 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 7;
        var typeM = 70200; // 电商, 淘宝banner

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, null, null, function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "淘宝banner",
                en: CONFIG.CHS_EN.getEn('淘宝banner',1),
                typeL: typeL,
                typeM: typeM,
                column: 1,
                direction: 0,
                list: data || []
            };
            return callback(error,data);
        });
    };

    // 热门微商
    tg3Util.get_hotsWechatBusiness_v6_0 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 8;
        var typeM = -1; // 电商, 淘宝banner
        // 获取一级分类对应的热门
        this.getHotItemCore_v6_0(typeL, typeL, pageNum, pageSize, null, null, function(error,data){
            // 热门与列表显示列数不同
            data = {
                title: "微商",
                en: CONFIG.CHS_EN.getEn('微商',1),
                typeL: typeL,
                typeM: typeM,
                column: 3,
                direction: 0,
                list: data || []
            };
            return callback(error,data);
        });
    };

    // 热门名片
    tg3Util.get_hotsBusinessCard_v6_0 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40100; // 印品,名片

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            // 热门与列表显示列数不同
            var data = {
                title: "名片",
                en: CONFIG.CHS_EN.getEn('名片',1),
                typeL: typeL,
                typeM: typeM,
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门传单
    tg3Util.get_hotsLeaflet_v6_0 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40200; // 印品,传单

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            // 热门与列表显示列数不同
            var data = {
                title: "传单",
                en: CONFIG.CHS_EN.getEn('传单',1),
                typeL: typeL,
                typeM: typeM,
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门展架
    tg3Util.get_hotsDisplayRack_v6_0 = function(callback){
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40300; // 印品,展架

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            // 热门与列表显示列数不同
            var data = {
                title: "热门展架",
                en: CONFIG.CHS_EN.getEn('热门展架',1),
                typeL: typeL,
                typeM: typeM,
                column: 2.2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门海报
    tg3Util.get_hotsPrintPoster_v6_0 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40500; // 印品, 海报

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            var data = {
                title: "海报",
                en: CONFIG.CHS_EN.getEn('海报',1),
                typeL: typeL,
                typeM: typeM,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };
    // 热门优惠券
    tg3Util.get_hotsPrintCoupon_v6_0 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40600; // 印品, 优惠券

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            var data = {
                title: "优惠券",
                en: CONFIG.CHS_EN.getEn('优惠券',1),
                typeL: typeL,
                typeM: typeM,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 热门不干胶
    tg3Util.get_hotsPrintSticker_v6_0 = function(callback) {
        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40700; // 印品, 不干胶

        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            var data = {
                title: "不干胶",
                en: CONFIG.CHS_EN.getEn('不干胶',1),
                typeL: typeL,
                typeM: typeM,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };
    // 热门停车牌
    tg3Util.get_hotsStopSign_v6_0 = function(callback) {

        var pageSize = 12;
        var pageNum = 0;
        var typeL = 4;
        var typeM = 40800; // 印品, 停车牌
        /**
         // 印品商品预览图宽高
         CONFIG.PRINT_ITEM_PREVIEW_WIDTH  = 344;
         CONFIG.PRINT_ITEM_PREVIEW_HEIGHT = 204;
         * */
        this.getHotItemCore_v6_0(typeL, typeM, pageNum, pageSize, CONFIG.PRINT_ITEM_PREVIEW_WIDTH, CONFIG.PRINT_ITEM_PREVIEW_HEIGHT, function(error,data){
            var data = {
                title: "停车牌",
                en: CONFIG.CHS_EN.getEn('停车牌',1),
                typeL: typeL,
                typeM: typeM,
                column: 2,
                direction: 0,
                list: data
            };
            return callback(null, data);
        });
    };

    // 微商分类
    tg3Util.get_wechatBusiness_v6_0 = function ( callback){
        var typeL = 8;//微商;
        return this.get_child_type_core(typeL,function(err,data){
            var newItem = {
                id: -1,
                name: '最新',
                en: 'NEW',
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/145834596_ZrEsDWtDHCjvUnEcU3Ax'// v5.5
            };

            if(data && data.length>0) {
                data.splice(0,0,newItem);
            } else {
                data = [newItem];
            }

            callback(err,data);
        });
    };

    //------------------------------------------------------------------
    /* 获取专题的内容列表
     * 参数：stId：专题id
     * 参数：pageNum：页码
     * 参数：pageSize：
     * 参数：callback：回调，参数如下：
     *          error:错误消息
     *          data:获取专题的内容列表的数组，数组元素如下：
     *                 type:
     *                 id: 模版id(印品时返回商品id)
     *                 thumb:
     *                 name:
     *                 vip
     */
    tg3Util.getSpecialTopicItems_v6_0 = function ( stId,pageNum,pageSize,callback){
        var entity = new TgSpecialTopicModel({
            st_id:stId
        });


        var pageSize = pageSize;
        var skipNum = pageNum*pageSize;
        entity.findRelItemsFromDB(function(error,items) {
            if (error) {
                console.log(error);
                return callback(error);
            }
            var adInfo = [];
            for(var i=0;i<items.length;i++){
                var obj = null;
                if(Number(items[i].type_id||'0')==4) {
                    obj = {
                        type: 4,
                        id: items[i].goods_id,
                        thumb: items[i].goods_thumb,
                        name: items[i].goods_name,
                        w:CONFIG.PRINT_ITEM_PREVIEW_WIDTH,
                        h:CONFIG.PRINT_ITEM_PREVIEW_HEIGHT,
                        typeL : 4,
                        title : items[i].goods_name,
                        desc : items[i].goods_desc,
                        useCount : items[i].show_apply_cnt||items[i].apply_cnt||0,
                        vip : items[i].vip || 0
                    };
                } else {
                    obj = {
                        type: items[i].type_id,
                        id: items[i].tmpl_id,
                        thumb: items[i].thumb,
                        name: items[i].tmpl_name,
                        w:items[i].width,
                        h:items[i].height,
                        typeL : items[i].type_id,
                        title : items[i].tmpl_name,
                        desc : items[i].goods_desc,
                        useCount : items[i].show_apply_cnt||items[i].apply_cnt||0,
                        vip : items[i].vip || 0
                    };
                }
                // vip 描述更改为 vip 专享, add by wangjun 2016-10-27
                if (obj.vip == 10) {
                    obj.desc = 'VIP专享';
                }
                adInfo.push(obj);
            }
            return callback(null, adInfo);
        },skipNum,pageSize, true);
    };

    /*
    * 获取合伙人分享模板相关信息
    *
    * @param id 分享项目唯一标识
    * @param typeL 一级分了
    * @param kind 是否为秒杀商品 10 普通 20 秒杀
    * @param callback 回调
    *           err: 错误信息
    *           data: {"itemInfo" : {"id" : 789,"thumbs" : ["http://oss-img.jpg", "http://oss-.jpg"],"typeL" : 4,"kind" : 10 }
    * */
    tg3Util.getCityPartnerShareItemInfo_v6_1 = function(id,typeL,kind,callback){
        //判断是否为秒杀商品
        var data = {id:id, typeL : typeL, kind : kind};
        if(kind == 20){
            //是秒杀商品
            activityUtil.getActItemContent(id, function(err,actItem){
                if(err) {
                    return callback(err, null);
                }
                data.thumbs = actItem.thumbs;
                return callback(err,data);
            });
            return;
        }
        /**
         // 1.单页  2.多页  3.ppt  4.印品  6.社交媒体  7.电商  8.微商 1010. logo 1011 营销商品
         CONFIG.DOC_TYPE_1 = 1;CONFIG.DOC_TYPE_2 = 2;CONFIG.DOC_TYPE_3 = 3;CONFIG.DOC_TYPE_4 = 4;CONFIG.DOC_TYPE_6 = 6;CONFIG.DOC_TYPE_7 = 7;CONFIG.DOC_TYPE_8 = 8;CONFIG.DOC_TYPE_1010 = 1010;CONFIG.DOC_TYPE_1011 = 1011;
         * */
        // 获取 ppt 缩略图
        if(typeL == CONFIG.DOC_TYPE_3) {
            var entity  = new ItbTmplPageModel({
                tmpl_ids: id
            });
            entity.getPagesInfoByTmplIds(function(error, items){
                if(error){
                    return callback(error,null);
                }
                if(!items || items.length <=0) {
                    return callback('无此项目信息',null);
                }
                data.thumbs = [];
                for(var i=0; i<items.length; i++) {
                    data.thumbs.push(items[i].thumb_s);
                }
                return callback(null, data);
            });
            return;
        }
        // 商品
        if(typeL == CONFIG.DOC_TYPE_4 || typeL == CONFIG.DOC_TYPE_1010 || typeL == CONFIG.DOC_TYPE_1011){
            (new ItbItemsModel()).findPrintItemAndUserInfo(id, function(error, items){
                if(error){
                    return callback(error,null);
                }
                if(!items || items.length==0){
                    return callback("无此项目信息",null);
                }
                var item = items[0];
                data.thumbs = JSON.parse(item.thumbs);
                return callback(null, data);
            });
            return;
        }
        // 设计
        if(typeL == CONFIG.DOC_TYPE_1 || typeL == CONFIG.DOC_TYPE_6 || typeL ==CONFIG.DOC_TYPE_7 || typeL == CONFIG.DOC_TYPE_8) {
            var entity = new TgDesignPageModel({
                design_id: id,
                del_flg:0
            });
            entity.getPageInfoById(function(error, items){
                if(error){
                    return callback(error, null);
                }
                if(!items || items.length == 0) {
                    return callback('无此项目信息', null);
                }
                data.thumbs = [];
                for (var i in items){
                    data.thumbs.push(items[i].thumb);
                }
                return callback(null,data);
            });
            return;
        }
        return callback('分类错误', null);
    };

    tg3Util.getCityPartnerShareSimilarItems_v6_1 = function(id,typeL,kind,callback){
        //判断是否为秒杀商品
        var data = [];
        if(kind == 20){
            // 去秒杀中查找
            return activityUtil.getSimilarActivityItemsByActItemId(id, 0, 12,callback);
        }
        /**
         // 1.单页  2.多页  3.ppt  4.印品  6.社交媒体  7.电商  8.微商 1010. logo 1011 营销商品
         CONFIG.DOC_TYPE_1 = 1;CONFIG.DOC_TYPE_2 = 2;CONFIG.DOC_TYPE_3 = 3;CONFIG.DOC_TYPE_4 = 4;CONFIG.DOC_TYPE_6 = 6;CONFIG.DOC_TYPE_7 = 7;CONFIG.DOC_TYPE_8 = 8;CONFIG.DOC_TYPE_1010 = 1010;CONFIG.DOC_TYPE_1011 = 1011;
         * */
        // 获取 ppt 缩略图
        if(typeL == CONFIG.DOC_TYPE_3) {
            // PPT
            (new ItbTemplateModel).getPptInfoByType(2,0,0,12,function(err,ret){
                if(err){
                    return callback(err, null);
                }
                var retData = [];
                for(var i=0;i<ret.length;i++){
                    retData.push({
                        id:ret[i].tmpl_id,
                        thumb:ret[i].thumb||'',
                        title:ret[i].tmpl_name||'',
                        desc:ret[i].tmpl_price||'',
                        typeL:typeL,
                        w:1600,
                        h:900,
                        useCount:ret[i].show_apply_cnt||ret[i].apply_cnt||0,
                        vip:ret[i].vip||0,
                        kind : 10
                    });
                }
                return callback(null,retData);
            });
            return;
        }
        // 商品
        if(typeL == CONFIG.DOC_TYPE_4 || typeL == CONFIG.DOC_TYPE_1010 || typeL == CONFIG.DOC_TYPE_1011D){
            return printUtil.getSimilarItems(id,0,12,callback);
        }
        // 设计
        if(typeL == CONFIG.DOC_TYPE_1 || typeL == CONFIG.DOC_TYPE_6 || typeL ==CONFIG.DOC_TYPE_7 || typeL == CONFIG.DOC_TYPE_8) {
            return tg3Util.getSimilarDesignDocItems(id,0,12,callback);
        }
        return callback('分类错误', null);
    };


    /**
     * 根据设计 id 获取相似的设计
     * @param id 设计唯一标识
     * @param pageNum 页码
     * @param pageSize 页容量
     * @param callback 回调
     *          err: 错误
     *          items: [{}]
     * */
    tg3Util.getSimilarDesignDocItems = function(id, pageNum, pageSize, callback) {
        (new TgDesignDocModel).getDesignTypeInfo(id, function(err,typeInfo){
            if(err) {
                return callback(err,null);
            }
            if(!typeInfo) {
                return callback('暂无分类信息',null);
            }
            (new TgDesignDocModel).getDesignInfoByType(typeInfo.typeL, typeInfo.typeM,0,pageNum*pageSize, pageSize,function(error, designs){
                var ret = [];
                for(var i in designs) {
                    ret.push({
                        id:designs[i].design_id,
                        thumb:designs[i].thumb||'',
                        title:designs[i].design_name||'',
                        desc:designs[i].tmpl_price||'',
                        typeL:typeInfo.typeL,
                        w:designs[i].width,
                        h:designs[i].height,
                        useCount:designs[i].show_apply_cnt||designs[i].apply_cnt||0,
                        vip:designs[i].vip||0,
                        kind:10
                    });
                }
                return callback(null,ret);

            },false);
        });
    };


    // 获取所有分类信息(一级分类套二级, 二级套三级)
    tg3Util.getAllTypeInfo = function(callback){
        var ret = [];
        // 1. 获取一级分类
        (new TgTypeModel).getCurrentTypeInfo(null,function(getTypeLErr,typeLs){
            if(getTypeLErr) {
                console.log("tg3Util.js [getAllTypeInfo]  getTypeLErr: " + getTypeLErr);
                return callback(getTypeLErr, null);
            }
            ret = typeLs;
            // 2. 获取二级分类
            var tasks = [];
            for(var i in ret) {
                tasks.push((function(index,typeL){
                    return function(callback) {
                        (new TgTypeModel).getCurrentTypeInfo(typeL.id,function(err, typeMs){
                            typeL.children = typeMs || [];
                            return callback(err,typeMs);
                        });
                    }
                })(i,ret[i]))
            }
            async.parallel(tasks,function(getTypeMErr,typeMs){
                if(getTypeMErr) {
                    console.log("tg3Util.js [getAllTypeInfo]  getTypeMErr: " + getTypeMErr);
                    return callback(getTypeMErr, null);
                }
                // 3.获取三级分类
                var funcs = [];
                for(var i in ret) {
                    //if(!ret[i].children || !ret[i].children.length <=0) continue;
                    for(var j in ret[i].children) {
                        funcs.push(
                            (function(i, j,typeM) {
                                return function(callback) {
                                    (new TgTypeModel).getCurrentTypeInfo(typeM.id,function(err,typeSs){
                                        typeM.children = typeSs || [];
                                        return callback(err,typeSs);
                                    });
                                }
                            })(i,j,ret[i].children[j])
                        )
                    }
                }
                async.parallel(funcs,function(getTypeSErr,typeSs){
                    if(getTypeSErr) {
                        console.log("tg3Util.js [getAllTypeInfo]  getTypeSErr: " + getTypeSErr);
                        return callback(getTypeSErr, null);
                    }
                    console.log("ret: " + JSON.stringify(ret));
                    return callback(getTypeSErr, ret);
                });
            });
        });
    };

    /**
     * 临时确认分享条目类型
     *
     * @param id 分享条目唯一标识
     * @param typeL 分享条目原始类型
     * @param callback 回调
     *          err: 错误类型
     *          id: 分享条目标识
     *          typeL 确认后的分类
     *
     * */
    tg3Util.checkRealShareItemType = function(id, typeL, callback) {

        // 1. 确认原始数据是否能够查到数据
        var entity = null;
        if(typeL == CONFIG.DOC_TYPE_3) {
            entity = new ItbTemplateModel({
                tmpl_id:id,
                del_flg:0
            });
        } else if(typeL == CONFIG.DOC_TYPE_4 || typeL == CONFIG.DOC_TYPE_1010 || typeL == CONFIG.DOC_TYPE_1011) {
            entity = new ItbItemsModel({
                item_id : id,
                del_flg : 0
            });
        } else {
            entity = new TgDesignDocModel({
                design_id: id,
                del_flg : 0
            });
        }
        entity.findFromDB(function(findDefaultErr, defaults){
            if(findDefaultErr) return callback(findDefaultErr, null,null);
            if(defaults.length > 0) return callback(null,id,typeL); // 原本的分类能查到, 就以原来的为准

            // 2. 查看是否属于商品
            var item = new ItbItemsModel({
                item_id : id,
                del_flg : 0
            });
            item.findFromDB(function(findItemErr, items){
                if(findItemErr) {
                    return callback(findItemErr,null,null);
                }
                if(items.length > 0) return callback(null, id, CONFIG.DOC_TYPE_4); // 商品能取到 就当成商品
                // 3. 查看是否属于模板
                var design = new TgDesignDocModel({
                    design_id: id,
                    del_flg : 0
                });
                design.findFromDB(function(findDesignErr, designs){
                    if(findDesignErr) {
                        return callback(findDesignErr, null,null);
                    }
                    if(designs.length >0) return callback(null, id, CONFIG.DOC_TYPE_1); // 设计模板能取到, 就当成设计为1

                        //4. 查看是否属于 ppt
                        var template = new ItbTemplateModel({
                            tmpl_id:id,
                            del_flg:0
                        });
                        template.findFromDB(function(findTemplateErr, templates){
                            if(findTemplateErr) {
                                return callback(findTemplateErr, null, null);
                            }
                            if(templates.length > 0) return callback(null, id, CONFIG.DOC_TYPE_3); // ppt 能取到, 就当成 ppt
                            return callback('该项目不存在', null, null);
                        });
                });
            });
        });
    };

    /**
     * 根据分享条目 id, 分享条目类型, 是否为秒杀商品, 获取分享信息
     *
     * @param userId 用户唯一标识
     * @param id 分享条目唯一标识
     * @param typeL 一级分类
     * @param kind 分享类型 10 普通模板/商品, 20 秒杀
     * @param callback
     *          err 错误信息
     *          data 分享条目信息
     * */

    tg3Util.getCityPartenerShareItemInfo = function(userId, id, typeL, kind, callback) {
        async.series([
                function(callback){
                    //获取当前商品/模版的信息
                    tg3Util.getCityPartnerShareItemInfo_v6_1(id,typeL,kind,function(error,data){ callback(error,data)});//*** results[0]
                },
                function(callback){
                    //获取与当前模版相近的模版信息
                    tg3Util.getCityPartnerShareSimilarItems_v6_1(id,typeL,kind,function(error,data){callback(error,data)});//*** results[1]
                }
            ],
            function(err, results) {
                if (err) {
                    return callback(err, null);
                }
                var data = {};
                data.itemInfo = results[0];
                data.similarItems = results[1];
                data.column = tg3Util.getAppShowColumn(typeL);
                data.userId = userId;
                // 超过两行, 按两行取
                if(data.similarItems.length >= data.column * 2) {
                    data.similarItems = data.similarItems.slice(0,data.column*2);
                }
                // 超过一行不到两行, 按一行取
                else if(data.similarItems.length >= data.column && data.similarItems.length < data.column * 2) {
                    data.similarItems = data.similarItems.slice(0,data.column);
                } else if(data.similarItems.length < data.column) {
                    data.similarItems = [];
                }

                return callback(err, data);
            });
    };

    /**
     * VI实验室信息
     *
     * */
    tg3Util.get_vilabInfo_v6_2 = function(callback) {
        var obj = {
            "thumb": "http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2017/09/01/114254900_X4P43oxtUNmNgnmUb4NW",
            "w": 750,
            "h": 240
        };
        // 注意: 与前端约定, 若隐藏 vi 实验室入口, 则在此处传递 null
        return callback(null, obj);
    };

    /**
     * 获取 app 开屏广告信息
     *
     * @param screenW app 所在设备宽
     * @param screenH app 所在设备高
     * @param callback 回调
     *            error: 错误信息
     *            data: 信息 {
     *                          "kind" : 1, // 开屏广告类型, 暂定 1 为广告图片显示, 当前版本若不为1则不处理
     *                          "url" : "", // 广告详情跳转页面(使用 banner 图的 webview, 暂不处理)
     *                          "imgUrl" : "http://oss-img.jpg", // 图片地址
     *                          "imgW" : 750, // 广告图宽
     *                          "imgH" : 1334, // 广告图高
     *                          "displayTime" : 3, // 开屏广告的展示默认时长(单位: 秒)
     *                          "displayInterval" : 30000000, // 两次开屏广告展示的最小间隔(单位: 秒)
     *                       }
     * */
    tg3Util.getAppAdInfo_v6_2 = function(screenW, screenH, callback){
        (new ItbNotificationModel()).getAppAdInfo(screenW, screenH, function(error, info){

            if(error || !info) {
                return callback(error || '暂无开屏广告信息', null);
            }
            // 如果类型为1, 广告图显示, 却没有图片链接, 返回失败
            if(info.kind== 1 && (!info.imgUrl || info.imgUrl.length <= 0)) {
                return callback('暂无开屏广告信息', null);
            }

            // 统一获取图片尺寸
            tg3Util.getOssImgSize(info.imgUrl, function(error, data){
                if(error) {
                    return callback(error, null);
                }
                info.imgW = Math.floor(data.w);
                info.imgH = Math.floor(data.h);

                var ossProcessPrefix = '?x-oss-process=image';

                // 图片的宽高比要是超出了屏幕的宽高比, 则调整图片比例至屏幕宽高比
                var imgR = info.imgW / info.imgH;
                var deviceR = screenW /screenH;
                if (deviceR > imgR) {
                    var h = Math.floor(info.imgW / deviceR);
                    info.imgUrl += info.imgUrl.indexOf(ossProcessPrefix) == -1 ? ossProcessPrefix : '';
                    info.imgUrl+='/crop,x_0,y_0,w_'+info.imgW+',h_'+h;
                    info.imgH = h;
                }
                // 屏幕宽度小于图片宽度则有缩放余地, 可将图片缩放至屏幕宽, 若屏幕宽度比图片还宽, 为了防止图片过大, 则不进行放大操作
                if (screenW < data.w) {
                    info.imgUrl += info.imgUrl.indexOf(ossProcessPrefix) == -1 ? ossProcessPrefix : '';
                    info.imgUrl+='/resize,w_'+screenW;
                    var w = Math.floor(screenW);
                    var h = Math.floor(w * info.imgH/ info.imgW);
                    info.imgW = w;
                    info.imgH = h;
                }
                // 经过比例调整, 宽度缩放后,
                if (screenH < info.imgH) {
                    info.imgUrl += info.imgUrl.indexOf(ossProcessPrefix) == -1 ? ossProcessPrefix : '';
                    info.imgUrl+='/crop,x_0,y_0,w_'+info.imgW+',h_'+Math.floor(screenH);
                    info.imgH = Math.floor(screenH);
                }




                return callback(null, info);
            });
        });
    };

    /* 获取图片尺寸：仅仅适用于阿里云oss
     * 参数：image: oss图片地址
     * 参数：callback，回调,参数如下：
     *      error:错误
     *      data:对象，结构如下：w:width
     *                         h:height
     */
    tg3Util.getOssImgSize = function(imgUrl, callback){
        if(!imgUrl){
            return callback(null,null);
        }

        var url = imgUrl+'?x-oss-process=image/auto-orient,1/info'; // 自动旋转到正向
        superagent.get(url)
            .end(function(err, res){
                if(err){
                    return callback(err, null);
                }
                var retJson = JSON.parse((res.text||'{}'));
                if(retJson && retJson.ImageHeight && retJson.ImageWidth){
                    var ret = {};
                    if(retJson.Orientation && (retJson.Orientation.value==6 || retJson.Orientation.value==8) ){
                        ret.w = retJson.ImageHeight.value*1;
                        ret.h = retJson.ImageWidth.value*1;
                    }else{
                        ret.h = retJson.ImageHeight.value*1;
                        ret.w = retJson.ImageWidth.value*1;
                    }
                    return callback(null, ret);
                }
                return callback(null, null);
            }
        );
    };

    /**
     * 获取 ppt 三级级分类信息
     *
     *
     * */
    tg3Util.get_pptType_v6_3 = function(callback) {

        var entity = new TgTypeModel({
            parent: 30100,
            state:3,
            del_flg:0
        });
        entity.findFromDB(function(error, results){
            if(error) {
               return callback(error, null);
            }
            var types=[];
            var newItem = {
                id: -1,
                name: '最新',
                en: 'NEW',
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/UI/IOS/V5.1/icon/1_2_all.png',
                //icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/04/17/145821250_piyCcowbMXmxe2NrFWqX',
                icon: 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/06/06/145834596_ZrEsDWtDHCjvUnEcU3Ax'// v5.5
            };
            types.push(newItem);

            // 重组类型
            for(var i=0; i<results.length; i++) {
                types.push({
                    id : results[i].type_id,
                    name: results[i].type_name,
                    icon : results[i].icon || '',
                    en: CONFIG.CHS_EN.getEn(results[i].type_name,1),
                })
            }


            callback(error,types);
        });
    };
    //------------------------------------------------------------------
    /* 获取app一行显示的列数 6.3
     * 参数: typeL	请求数据的一级分类  1 海报/3 ppt/4 印品/6社交媒体
     * 参数: typeM	请求数据的二级分类(-1 标示最新)
     * 参数[可选]: typeS	请求数据的三级分类, 若不存在标识仅以一级二级分类请求数据
     * 返回值: 列数
     */
    tg3Util.getAppShowColumn_v6_3 = function(typeL, typeM, typeS){
        var typeLClu =  { "1":2,"3":2,"4":2,"6":2, "8" : 2};
        var typeMClu =  { "60100":1,"60200":2,"60300":1, "70100" : 2, "70200" : 1, "40600" : 1,"40800":1};
        var typeSClu =  {};
        return typeSClu[typeS]
            || typeMClu[typeM]
            || typeLClu[typeL]
            || 2;
    }
    /**
     * 获取 app 6.3 分类信息
     *
     *
     * */
    tg3Util.get_appTypesInfo = function(callback) {
        async.series([
            // 单页分类
            function(callback){ //*** results[0]
                tg3Util.get_poseterTypeM_v5_1(function(error,data){ callback(error,data)});
            },
            // 社交媒体分类
            function (callback) {  //*** results[1]
                tg3Util.get_socialMediaTypeM_v5_3(function(error,data){callback(error,data)});
            },
            // 电商分类
            function (callback) {  //*** results[2]
                tg3Util.get_eBusinessTypeM_v5_7(function(error,data){callback(error,data)});
            },
            // 微商分类
            function (callback) { //*** results[3]
                tg3Util.get_wechatBusiness_v6_0(function(error,data){callback(error,data)});
            },
            // 印品分类
            function (callback) { //*** results[4]
                tg3Util.get_printTypeM_v5_7(function(error, data){callback(error,data)});
            },
            // ppt 分类
            function(callback){ //*** results[5]
                tg3Util.get_pptType_v6_3(function(error,data){ callback(error,data)});
            }
        ], function(error, results){

            /**
             * kind: -1, 标记不是分类, 1 标记一级分类, 2 标记二级分类, 3 标记三级分类
             * */
            var design = {id : -1, kind:-1, name : '线上设计', en : ''};
            var print  = {id : 4, kind:1, name : '线下印刷', en : ''};
            var work   = {id : -1, kind:-1, name : '效率办公', en : ''};

            print.children = results[4] || [];
            for(var i =0; i < print.children.length; i++) {
                print.children[i].column = tg3Util.getAppShowColumn_v6_3(print.id, print.children[i].id);
                print.children[i].children = [];
                print.children[i].kind = 2; // 印品的 children 是二级分类
            }

            var types = [design, print, work];

            var poster          = {id : 1, kind:1, name : '单页', en : ''};
            var socialMedia     = {id : 6, kind:1, name : '社交媒体',en : ''};
            var eBusiness       = {id : 7, kind:1, name : '电商',en : ''};
            var wechatBusiness  = {id : 8, kind:1, name : '微商', en : ''};
            var ppt             = {id : 3, kind:1, name : '幻灯片', en : ''};

            poster.children = results[0] || [];
            socialMedia.children = results[1] || [];
            eBusiness.children = results[2] || [];
            wechatBusiness.children = results[3] || [];
            ppt.children = results[5] || [];

            var maxLen = 0;
            maxLen = maxLen > poster.children.length ? maxLen : poster.children.length;
            maxLen = maxLen > socialMedia.children.length ? maxLen : socialMedia.children.length;
            maxLen = maxLen > eBusiness.children.length ? maxLen : eBusiness.children.length;
            maxLen = maxLen > wechatBusiness.children.length ? maxLen : wechatBusiness.children.length;
            maxLen = maxLen > ppt.children.length ? maxLen : ppt.children.length;

            for(var i=0; i < maxLen; i++) {
                if(i<poster.children.length) {
                    poster.children[i].kind = 2; // 单页的 children 是二级分类
                    poster.children[i].column = tg3Util.getAppShowColumn_v6_3(poster.id, poster.children[i].id);
                    poster.children[i].children = [];
                }
                if(i<socialMedia.children.length) {
                    socialMedia.children[i].kind = 2; //社交媒体的 children 是二级分类
                    socialMedia.children[i].column = tg3Util.getAppShowColumn_v6_3(socialMedia.id, socialMedia.children[i].id);
                    socialMedia.children[i].children = [];
                }
                if(i<eBusiness.children.length) {
                    eBusiness.children[i].kind = 2; // 电商的 children 是二级分类
                    eBusiness.children[i].column = tg3Util.getAppShowColumn_v6_3(eBusiness.id, eBusiness.children[i].id);
                    eBusiness.children[i].children = [];
                }
                if(i<wechatBusiness.children.length) {
                    wechatBusiness.children[i].kind = 2; // 微商的 children 是二级分类
                    wechatBusiness.children[i].column = tg3Util.getAppShowColumn_v6_3(wechatBusiness.id, wechatBusiness.children[i].id);
                    wechatBusiness.children[i].children = [];
                }
                if(i<ppt.children.length) {
                    ppt.children[i].kind = 3; // PPT children 是二级分类
                    ppt.children[i].column = tg3Util.getAppShowColumn_v6_3(ppt.id, ppt.children[i].id);
                    ppt.children[i].children = [];
                }
            }
            design.children = [poster,socialMedia,wechatBusiness,eBusiness];
            work.children = [ppt];
            return callback(null,types);
        });
    };
    return tg3Util;
})();
//------------------------------------------------------------------
module.exports = tg3Util;



